Jump to content
Sign in to follow this  
jbrouwer

Custom Taxonomy In Permalink

Recommended Posts

jbrouwer    5
jbrouwer

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
Rob    547
Rob

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


Former PageLines Moderator, Food Expert and Raconteur

Share this post


Link to post
Share on other sites
jbrouwer    5
jbrouwer

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
jbrouwer    5
jbrouwer

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
Rob    547
Rob

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.


Former PageLines Moderator, Food Expert and Raconteur

Share this post


Link to post
Share on other sites
jbrouwer    5
jbrouwer

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • aldisney
      By aldisney+
      Hi--
      I was wondering if there was a relatively easy to way automatically assign a thumbnail to a post category when displaying posts in the Masonic Gallery format, when the post doesn't have a featured image? I've got a client with a ton of posts that just won't have any featured images, but each category has a header image. I'd like to display the header image as the thumbnail for all these posts, since, going forward, my client does actually plan to use featured images.
       
      Thanks!
    • marc.stubbe
      By marc.stubbe
      i created more than one page inspiration, with a result that the only current page inspiration is named: http://www.vmhdigital.nl/inspiration-3/
       
      I emptied the garbage bin, but cannot rename this permalink, do you know how to solve that?
    • jonesbre10
      By jonesbre10
      Hi,
       
      I am recieving the same error message for all of my posts "404! Sorry this page does not exist" and Im not sure what I did to cause it. I was able to view the posts fine recently.
       
      The only thing I have noticed is that all of my posts have the same permalink now which looks something like "www.website.com/rock". I admit I was messing around with permalinks for the different pages but I dont know now how to reset the posts or any permalinks. Even new posts now make this permalink connection with "www.website.com/rock".
       
      Any advice would help I dont know much about permalinks and would be happy just to reset them.
    • seattle5021209
      By seattle5021209
      Hello and thank you for your help with these questions.
       
      I have two questions, but they are tied to one another so I decided to post them together.
       
      I would like to replicate the way the following website has set up their posts:
       
      http://www.loopinsight.com
       
      1. Straight Link to 3rd party sites - As you can see, if they are linking to a 3rd party article, they credit the article and when you click on the post title it takes you straight to an outside website [straight link], and not to the loopinsight.com post permalink.  
       
      I would like to set up my posts this way.  If I am writing my own content post, when visitors click on the title they should be taken to the post's permalink.  However, if I am linking to a 3rd party site post that I found interesting, when visitors click the title it should take them straight to that 3rd party site.
       
       
      2. Permalink Button - I would like to recreate the little loop besides every post [with my own button of course].  This little loop button takes you the post's permalink [http://www.loopinsight.com/...].  
       
      I would like to display a corresponding "Permalink" button next to each of my posts.
       
       
       
      Thank you so much for your help.
       
       
    • Anthony Luth
      By Anthony Luth
      I am working on a project that utilizes both Blog category and category name. I am trying to have the blog archive page list only posts for the Blog category and an additional archive for posts from other categories, for example:
       
      If a post is from category Sports, it shows on somedomain.com/category/sports or somedomain.com/sports/somepost and not under somedomain.com/blog and so on, and so forth. This is not added right now natively, so I will need to build a custom template to do this, but this could be a most beneficial addition to the pagelines frame work.
×