如何解决WordPress:添加目录以使用功能发布 slug
我想在每个 post slug 之前添加一个 /news/
。像这样:
example.com /news/ post-slug/
如果我在永久链接设置中添加 /news/
,它之后的每个自定义帖子类型都会被破坏。
我尝试将以下内容添加到每个 register_post_type
:
'with_front' => false,
但这无济于事。
我还尝试了 here 中的代码:
$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' ),
到您的自定义帖子类型参数。 所有帖子现在都将具有“新闻”固定链接,而不会影响其他帖子。
,我找到了答案。您必须在 with_front
中添加 rewrite
属性。
像这样:
'rewrite' => array( 'slug' => 'custom','with_front' => false ),
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。