Archived

This topic is now archived and is closed to further replies.

  • 0

Custom Taxonomy In Permalink


Question

Posted · Report post

Hi,

 

I've created a custom taxonomy (for location) and I would like to have this taxonomy in my permalink. 

 

Now I have: %category%/%postname% and I would like to have %location%/%category%/%postname%.

 

Via google I found this code:

 

add_filter('post_link', 'location_permalink', 10, 3);
add_filter('post_type_link', 'location_permalink', 10, 3);

function location_permalink($permalink, $post_id, $leavename) {
    if (strpos($permalink, '%%location%') === FALSE) return $permalink;

        // Get post
        $post = get_post($post_id);
        if (!$post) return $permalink;

        // Get taxonomy terms
        $terms = wp_get_object_terms($post->ID, 'location'); 
        if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
        else $taxonomy_slug = 'location';

    return str_replace('%location%', $taxonomy_slug, $permalink);
}

 

 

It is working for posts. However, I get 404 errors on pages. Hence, there is some permalink conflict. Any suggestion to fix this? 

Share this post


Link to post
Share on other sites

5 answers to this question

Posted · Report post

I'm sorry if I misunderstood you or if I failed to explain that pages don't normally use custom taxonomies, and thus the permalinks are unlikely to reflect them.  I think the reason it does with the post-id first is that it's then treating the custom taxonomy as a category after the post-id.   Perhaps if you swapped the category and location around, the same effect may occur without the post-id.

 

This is a WordPress issue and there may be some topics in their forum that may of greater help.  It really isn't PageLines related.

Share this post


Link to post
Share on other sites

Posted · Report post

Hello Jan,

 

Personally, I use Custom Press for creating taxonomies, which allows me to integrate them into posts, pages and even media files.

 

However, in another site where I've needed categories attached to pages (not a usual thing), I've used this plugin.  http://wordpress.org/extend/plugins/map-categories-to-pages/

 

It might help you to achieve your goal. It's well maintained by its developers.

 

You may wish to continue on the customization path you've already taken, without plugins, so this might explain how to register the taxonomy for pages.  http://justintadlock.com/archives/2009/11/09/excerpts-and-taxonomies-for-pages-in-wordpress-2-9

Share this post


Link to post
Share on other sites

Posted · Report post

Hi,

 

Thank you for your time.

 

I am fine using the register taxonomy function. Maybe I was not completely clear. I don't need a custom taxonomy for pages (you can set the taxonomy to work with pages in the above function if you want). I just would like the permalink structure for my posts to contain the location taxonomy. I was able to do so with the code from my first post, but it generates 404 errors for pages, strange as I thought pages are on top of the tree. 

 

Maybe I could go they way you are suggesting, but it doesn't sound dynamic to me. I would have to create a page each time I add a location. 

 

Anu other ideas?

Share this post


Link to post
Share on other sites

Posted · Report post

Just discovered that /%post_id%/%locatie%/%category%/%postname% resolves the 404 errors on pages, but I do not want a post_id in my url... :-)

Share this post


Link to post
Share on other sites

Posted · Report post

Hi,

 

I realize it is a wordpress issue, but just hoped someone here had the same issue :-)

 

Actually. Category/location/postname is working (hence, not giving 404 errors on pages). However, it is not a logical structure for my site... It is driving me crazy. Sound like a pretty simple (and common!) option to me.

 

Thank you for your help.

Share this post


Link to post
Share on other sites