0

How to create a permalink structure which finish by:

/category-name/post-name/

Instead of:

/category-slug/post-name/

By default, Wordpress is offering %category% tag strucure as "A sanitized version of the category name (category slug field on New/Edit Category panel".

How can I use the category name instead of category slug in permalinks?

Note that I dont need the easiest solution of making my slugs equal to my categories names. In fact, customer need to see in the area of edit post : the categories in a specific non-alpahbetical order. So I managed to do this by giving a digital number for the category as a slug and order showing categories by slug in an ASC mode using:

add_filter( 'get_terms_args', 'wpse_53094_sort_get_terms_args', 10, 2 );
function wpse_53094_sort_get_terms_args( $args, $taxonomies ) 
{
    global $pagenow;
    if( !is_admin() || ('post.php' != $pagenow && 'post-new.php' != $pagenow) ) 
        return $args;

    $args['orderby'] = 'slug';
    $args['order'] = 'ASC';

    return $args;
}

I think If I dont manage to use t f solution, I will look for a code or a plugin that adds meta field for categories and order by this field in ASC mode. Then I will rename my slugs to the same name of categories. What do you think about this approach?

Thank you for your comment

3
  • Just out of curiosity, can't you simply edit your category slugs so that they match your category names? Apr 19, 2013 at 16:43
  • I am answering your important question as an Edit note. thank you for your time. Apr 19, 2013 at 16:55
  • For hierarchy purposes, another USEFUL way: use Custom_post_types instead of Categories, like here:<br/> wordpress.stackexchange.com/questions/28979/…
    – T.Todua
    Nov 10, 2014 at 10:31

1 Answer 1

2

I did not test this, but this should do what you want. Put the following in your functions.php:

add_filter('rewrite_rules_array', 'new_category_name_rewrite_rule');
function new_category_name_rewrite_rule($rules) {
    $new_rules = array();
    $categories = get_categories();
    foreach ($categories as $category) {
        $cat_name = preg_replace('#\s+#', '-', $category->name);
        $new_rules['/'.$category->slug.'/'] = '/'.urlencode($cat_name).'/';
    }
    return $new_rules + $rules;
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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