我最近看到了一些帖子,上面有人很容易就把鼻涕虫从a single 通过以下功能自定义帖子类型:
function hf_remove_slug( $post_link, $post, $leavename ) {
if((\'pop-tags\' == $post->post_type || \'furniture-type\' == $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\', \'hf_remove_slug\', 10, 3 );
function hf_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\', \'pop-tags\', \'furniture-type\', \'page\' ) );
}
}
add_action( \'pre_get_posts\', \'hf_parse_request\' );
然而,当我添加多个自定义帖子类型时,我无法实现这一点。有人能帮忙/解释一下吗?
最合适的回答,由SO网友:jbwagner 整理而成
看起来你的if做了与你想要的相反的事情。如果在“家具类型”或“流行标签”上,则返回未更改的链接。
这应该可以:
function hf_remove_slug( $post_link, $post, $leavename ) {
if((\'pop-tags\' == $post->post_type || \'furniture-type\' == $post->post_type) && \'publish\' == $post->post_status) {
$post_link = str_replace( \'/\' . $post->post_type . \'/\', \'/\', $post_link);
return $post_link;
}
return $post_link;
}