a work on process

This is the blog of James Stewart, a London-based web developer. You can find out more about my work on the Ket Lai site, follow me on twitter, or find me on github, flickr, last.fm or bkkeepr.

Wordpress and “Pathless Categories”

You are reading a post by James Stewart entitled Wordpress and “Pathless Categories”. It was posted on 29 April 2009 at 11:25 am.

You can find more posts by returning to the index.

Filed under: Snippets
Tagged: , , , , , ,

I’m working on a wordpress project at the moment, and pushing that blogging engine quite a bit further than I have before. We’re going to be using categories very extensively and one of the first tasks has been to allow category paths without any preceding /category/ or the like. By default, wordpress wants the category with a slug of ‘case-studies’ to live at:

/category/case-studies

but we want it to be simply:

/case-studies

So far, so straightforward. I installed the pathless-category-links plugin and all was well. Until I started using subcategories. All subcategory links started returning 404s. It seems I’m far from alone in that problem, but I’ve not yet seen a solution offered, so a little digging was required.

What was happening with subcategories (at least in my setup) was that the wordpress method that parses the query string was identifying the top-level category name as the category name and the subcategory name as the ‘name’ (ie. the post slug to look for). What was needed was a check to see whether that ‘name’ maps to a category slug and if so, correct wordpress’ assumption.

The code I’m using is:

if (! function_exists('jys_pathless_category_links_query_string')) {
  function jys_pathless_category_links_query_string($qs) {
    parse_str($qs, $query_vars);
    if (isset($query_vars['name']) && get_category_by_slug($query_vars['name'])) {
      $res = array('category_name' => $query_vars['category_name'] . "/" . $query_vars['name']);
      return http_build_query($res);
    }
    return $qs;
  }
}
 
add_filter('query_string', 'jys_pathless_category_links_query_string');

(NB: This will break if you have permalinks set up as recommended by the plugin author (‘/%category%/%postname%’) and a post in your top-level category with the same slug as your subcategory. But hopefully that’s rare enough that we’ll be okay!)

Recommend this post:

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Related Posts

 

4 Comments

  • Hi, thanks for posting this solution – I’m having the same problem right now. However, where exactly do you put this amended code?

    Comment by Aaron — 8 May 2009 @ 5:55 pm

  • Oop, I just figured it out – you simply add it at the very end of the plugin. It seems to be working perfectly, thank you!

    Comment by Aaron — 8 May 2009 @ 5:57 pm

  • Thanks for this…

    but where exactly do I put the code etc?

    If I download the plugin – what do I need to do to the code?

    Thanks for your help

    Comment by Simon — 27 May 2009 @ 2:36 pm

  • @Simon – sorry, I’d forgotten to approve the two comments from Aaron so you wouldn’t have had the help from them.

    As Aaron said, you simply add it at the end of the plugin PHP file and it should work from there.

    Comment by James Stewart — 30 May 2009 @ 10:38 am

Sorry, the comment form is closed at this time.