为特定帖子类别创建自定义永久链接。我得到了这个,它工作得很好,除了,它是执行重定向,而不是重写。查看URL栏时,仍应显示:example.com/new_slug/name-of-my-post
不重定向到example.com/name-of-my-post
// Changes permalink to add new path, if in this category
add_filter( \'post_link\', \'pl_custom_permalink\', 10, 3 );
function pl_custom_permalink( $permalink, $post, $leavename ) {
global $post;
if ( has_category( \'My Category\', $post->ID) ) {
$permalink = trailingslashit( home_url(\'/new_slug/\'. $post->post_name . \'/\' ) );
}
return $permalink;
}
add_action(\'init\', \'pl_custom_rewrite_rules\');
function pl_custom_rewrite_rules( $wp_rewrite ) {
add_rewrite_rule( \'^/new_slug/(.*)$\', \'index.php?name=$matches[1]\', \'top\' );
}
我错过了什么?