Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
    
Just out of curiosity, can't you simply edit your category slugs so that they match your category names? –  WPMU-DEV Ari Apr 19 '13 at 16:43
    
I am answering your important question as an Edit note. thank you for your time. –  whiteletters and blankspaces Apr 19 '13 at 16:55

1 Answer 1

up vote 2 down vote accepted

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;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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