单个帖子类别/%category%/%postname%/的WordPress自定义url结构

时间:2019-05-16 15:01:05

标签: wordpress wordpress-theming permalinks

有没有办法让一个类别包含此url结构? www.website.com/%category%/%postname%/当我的设置设置为“帖子名称”时?

我只需要一个类别的结构。

我目前有这个,但是重定向不起作用。

function custom_permalink( $permalink, $post, $leavename ) {
    // Get the categories for the post
    $category = get_the_category($post->ID); 
    if ( !empty($category) && $category[0]->cat_name == "Accident News" ) {
        $permalink = trailingslashit( home_url('/accident-news/'. $post->post_name .'/' ) );
    }
    return $permalink;
}
function wp_insertMyRewriteRules($rules){
    $newrules = array();
    $newrules['^accident-news/(.*)$'] = 'index.php?name=$matches[1]';
    return $newrules + $rules;
}
function flushRules(){
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
    return $wp_rewrite;
}
add_filter('post_link', 'custom_permalink', 10, 3 ); 
add_filter('rewrite_rules_array','wp_insertMyRewriteRules');
add_filter('init','flushRules');

0 个答案:

没有答案