在永久链接中使用类别名称而不是类别slug

时间:2013-04-13 08:37:36

标签: wordpress mod-rewrite categories slug permalinks

如何创建一个完整的永久链接结构:

/category-name/post-name/

而不是:

/category-slug/post-name/

默认情况下,Wordpress提供%category%标记结构为"A sanitized version of the category name (category slug field on New/Edit Category panel"

如何在永久链接中使用类别名称而不是类别slug?

2 个答案:

答案 0 :(得分:0)

add_filter('rewrite_rules_array', 'category_name_rewrite_rule');
function 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}.'/'] = '/'.url_encode($cat_name).'/';
    }
    return $new_rules + $rules;
}

答案 1 :(得分:0)