我有一个自定义的帖子类型项目
现成的url是:
www.myapp。com/project/POST-ID
和
www.myapp。com/项目/后期SLUG
现在,我的客户端需要在根地址中提供url。因此,我向函数中添加了以下代码。php
function na_remove_slug( $post_link, $post, $leavename ) {
if ( \'project\' != $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\', \'na_remove_slug\', 10, 3 );
function na_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\', \'project\', \'page\' ) );
}
}
add_action( \'pre_get_posts\', \'na_parse_request\' );
现在可以了:
www.myapp。com/POST-SLUG
但这不起作用:
www.myapp。com/POST-ID
我有什么办法可以让它工作吗?