4

How can I change the category slug that appears in the URL of a post that's in multiple categories? I'd like to change the default WordPress behaviour of using the category with the lowest id.

I thought this would do it:

add_filter('post_link', 'mspc_post_link', 1, 2);
function mspc_post_link($link, $post) {
    if (strpos($link, 'bad-category-slug')) {
        $cats = get_the_category($post->ID);
        foreach ($cats as $cat) {
            if ("Bad Category Name" != $cat->cat_name) {
                $slug = $cat->category_nicename;
                break;
            }
        }
        $link = str_replace('bad-category-slug', $slug, $link);
    }
    return $link;
}

But while this kind of does work as expected, it doesn't change the URL visible to user.

1

Similar plugin but compatible to latest wp version 3.5.2

http://wordpress.org/plugins/wp-category-permalink/

This plugin allows you to select a 'main' category for your posts, for better permalinks and SEO. It uses the same meta data as the "Hikari Category Permalink" and the "sCategory Permalink", but it has been rewritten using better and cleaner code.

Requires: WP 3.5 or higher Compatible up to: 3.5.2 Last Updated: 2013-4-18

2
+50

Even though this question is pretty old, and you've probably long moved on, I figured I'd offer a solution anyway :-).

Check out http://wordpress.org/extend/plugins/hikari-category-permalink/, which allows you to do exactly what you're asking.

Cheers~

3
  • @bungeshea plugin warning seems to be like this > This plugin hasn't been updated in over 2 years. It may no longer be maintained or supported and may have compatibility issues when used with more recent versions of WordPress.
    – Muhammed
    Nov 30 '12 at 11:07
  • 1
    @maanu Ignore the warning, it still works with WP 3.5
    – shea
    Nov 30 '12 at 20:31
  • @bungeshea yes, it works :) Jazkallah khair
    – Muhammed
    Dec 1 '12 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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