WordPress:添加目录以发布具有功能的slug



我想在每个后段塞之前添加一个/news/。像这样:

example.com/news/post-slug/

如果我在永久链接设置中添加/news/,那么每个自定义帖子类型都会在它之后被破坏

我尝试将以下内容添加到每个register_post_type:

'with_front'        => false,

但这无济于事。

我还尝试了这里的代码:

$post_type = <POST TYPE NAME> # Define your own as a string
$new_slug = <WANTED SLUG> # Define your own as a string
add_filter( 'register_post_type_args', function($args, $post_type){
if ( 'post_type' == $post_type )
$args['rewrite'] = array( 'slug' => $new_slug );
return $args;
}, 10, 2 );

不幸的是,这也不起作用。

有没有办法在不破坏其他帖子类型的情况下添加/news/

这是我的自定义帖子类型的当前代码:

add_action( 'init', 'cpt_custom' );
function cpt_custom() {
register_post_type( 'custom',
array(
'rewrite'           => array( 'slug' => 'custom' ),
'hierarchical'      => true,
'public'            => true,
'has_archive'       => true,
'show_in_rest'      => true,
'with_front'        => true,
)
);
}

在永久链接设置中,它被设置为/news/%postname%/

您可以通过添加来重写自定义post-type slug

'rewrite' => array( 'slug' => 'news' ),

到您的自定义post-type参数。所有帖子现在都将有"新闻"永久链接,而不会影响其他帖子。

我找到了答案。您必须在rewrite中添加with_front属性。

像这样:

'rewrite' => array( 'slug' => 'custom', 'with_front' => false ),

最新更新