需要做两件事。首先,使用post_link
滤器
function _20170117_post_link( $url, $post, $leavename ) {
if( in_category( \'news\', $post) ) {
$url = get_site_url() . \'/news/\' . $post->post_name ;
}
return $url;
}
add_filter( \'post_link\', \'_20170117_post_link\', 10, 3 );
其次,为此制定重写规则:
function _20170117_rewrite() {
add_rewrite_rule(\'^news/(.?.+?)(?:/([0-9]+))?/?$\', \'index.php?pagename=$matches[1]
&page=$matches[2]\', \'top\');
add_rewrite_rule(\'^news/([^/]+)(?:/([0-9]+))?/?$\', \'index.php?name=$matches[1]
&page=$matches[2]\', \'top\');
flush_rewrite_rules();
}
add_action(\'init\', \'_20170117_rewrite\');
请注意
flush_rewrite_rules();
您不需要每次都执行。就一次。或者你可以去
options-permalink.php
然后单击保存。
我使用以下设置进行了测试: