如何解决删除自定义帖子类型 Slug 但如果有父级,请将其保留在 URL 中
我注册了一个自定义帖子类型,重写设置为 'rewrite' => array( 'with_front' => true )
,我用这个删除了它:
function my_remove_slug( $post_link,$post,$leavename ) {
if ( 'content_hub' != $post->post_type || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( '/' . $post->post_type . '/','/',$post_link );
return $post_link;
}
add_filter( 'post_type_link','my_remove_slug',10,3 );
function my_parse_request( $query ) {
if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type',array( 'post','content_hub','page' ) );
}
}
add_action( 'pre_get_posts','my_parse_request' );
这对于不包含父级的 URL 非常有效,但它也会重写包含父级的 URL。
例如:
mydomain.com/parent-slug/child-slug
这将返回 404。
但是没有父段加载的相同网址。
我需要更改它,以便父 slug 仍然包含在 URL 中,如果它存在。
我尝试过这样的事情:
function my_remove_slug( $post_link,$leavename ) {
if ( 'content_hub' != $post->post_type || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( '/' . $post->post_type . '/','/' . $parent_slug . '/',3 );
但这将返回网址:
mydomains.com/parent-slug/parent-slug/child-slug
仍然显示 404。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。