Hopefully another simple one, I have some nested categories, like this:

- category-a
    - category-b
         -category-c

The permalink for category-c is as follows:

http://<domain>/category/category-a/category-b/category-c/

What I'd really like is for the permalink to not include any parent categories, instead it would become this:

http://<domain>/category/category-c/

Is there a way this can be achieved? I have some categories that do nothing but group sub-categories together, thus I don't really want them displaying to my users.

Many thanks!

link|improve this question

71% accept rate
We also have a related question aboute removing the parents and the /category/ prefix. This is more complicated because it requires extra rewrite rules, and includes redirection for old-style links. – Jan Fabry Jan 26 '11 at 12:24
feedback

3 Answers

up vote 3 down vote accepted

The following code changes all links to category archives so they don't include the parent category:

add_filter( 'category_link', 'wpse7807_category_link', 10, 2 );
function wpse7807_category_link( $catlink, $category_id )
{
    global $wp_rewrite;
    $catlink = $wp_rewrite->get_category_permastruct();

    if ( empty( $catlink ) ) {
        $catlink = home_url('?cat=' . $category_id);
    } else {
        $category = &get_category( $category_id );
        $category_nicename = $category->slug;

        $catlink = str_replace( '%category%', $category_nicename, $catlink );
        $catlink = home_url( user_trailingslashit( $catlink, 'category' ) );
    }
    return $catlink;
}
link|improve this answer
That's exactly what I was after, does exactly what I need without editing any core files and without using a plugin (which is preferable for me). Many thanks @Jan Fabry – Ben Everard Jan 26 '11 at 13:00
Will your code work for removing parent category page permalink as well? – Mars Mar 22 '11 at 16:48
@Mars: I'm sorry, I don't understand what you want to ask. If you have a subcategory at /category/fruit/banana/ the links will be changed to /category/banana/. The /category/fruit/ archive will stay at that location. – Jan Fabry Mar 22 '11 at 17:19
feedback

Alas that won't work, although I did briefly look at this advanced permalinks wordpress plugin.

http://bit.ly/d9WWr

Does seem to mention more control over category level permalinks, maybe something to check out.

link|improve this answer
This is not a classic forum, you can add a comment to your own answer, or even better: you can edit your answer to improve it, and delete this answer. Now the chronology between your two answers can get lost as they get voted up or down. – Jan Fabry Jan 26 '11 at 12:06
Cheers Phil, I'll take a look at it. – Ben Everard Jan 26 '11 at 12:12
feedback

Damn, so Toby Howarth just found this link for me, a bit of a hack but it does what I need it to.

http://wordpress.org/support/topic/remove-parent-category-from-permalink#post-1038484

It involves changing part of wp-includes/category-template.php, not that I'm keen on using hacks but I really don't want the parent categories appearing in the category permalink structure.

link|improve this answer
Editing a core file is not a good idea, it will break when you upgrade. This should very easy to do with the category_link hook, I'll try it and post it back as an answer (unless someone else is quicker). – Jan Fabry Jan 26 '11 at 12:10
Cheers Jan, that would be most appreciated. I agree about editing the core file, I'd prefer not to. – Ben Everard Jan 26 '11 at 12:12
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.