您可以很容易地使CPT具有“无栓”永久链接:
register_post_type( \'service\', [
\'hierarchical\' => true,
\'public\' => true,
// Other args, no need for "rewrite"
]);
// Change service to top-level permalink.
add_permastruct( \'service\', \'%service%\', [
\'with_front\' => false,
\'paged\' => false,
\'feed\' => false,
]);
// Remove all default service rewrite rules.
add_filter( \'service_rewrite_rules\', \'__return_empty_array\' );
。。。应该将其包装在函数中并连接到
init
. 然后要“抓住”这些帖子类型,我们需要一点
pre_get_posts
魔术:
add_action( \'pre_get_posts\', function ( $wp_query ) {
if (
$wp_query->is_main_query() &&
$wp_query->is_singular() &&
$wp_query->get( \'pagename\' )
) {
if ( ! $post_type = $wp_query->get( \'post_type\' ) )
$post_type = [];
elseif ( ! is_array( $post_type ) )
$post_type = [ $post_type ];
$wp_query->set( \'post_type\', array_merge( $post_type, [
\'page\',
\'service\',
]));
}
});
然后确保刷新重写规则(只需在管理中加载设置>永久链接)。