我创建了一个自定义帖子,我不希望他们有URL。
有这样一个URL=站点。com/自定义slug/和id或标题
我不想要这个,我怎么能做到?
已经谢谢你了!
如果我正确理解了您提出的问题,那么您不希望您的CPT直接在前端可用。如果是,那么只需设置\'public\' => false
注册post\\u类型时。
请记住,其他各种参数register_post_type() 默认值为public
(例如。,show_ui
), 设置时显示\'public\' => false
您经常需要将这些参数显式设置为true,例如:。
$args = array (
// don\'t show this CPT on the front-end
\'public\' => false,
// but do allow it to be managed on the back-end
\'show_ui\' => true,
// other register_post_type() args
) ;
register_post_type (\'my_cpt\', $args) ;
Sparrow Hawk是正确的,因为“public”会影响其他几个args选项。因此,虽然他的解决方案会起作用,但如果您想避免由于其他一切都在起作用而不得不进行其他add\'l更改,只需将您想要的arg设置为false,即:
\'publicly_queryable\' => false,
将这一行添加到register\\u post\\u type函数中,创建自定义post类型,您就不必对代码进行任何其他更改。而且在进行此更改后,请确保刷新永久链接,否则您将看不到任何更改的迹象。
(要刷新它们,请转到设置/永久链接并单击保存)
这就是您想要的:
/**
* Remove the slug from custom post type permalinks.
*/
function wpex_remove_cpt_slug( $post_link, $post, $leavename ) {
if ( ! in_array( $post->post_type, array( \'YOUR_POST_TYPE_NAME\' ) ) || \'publish\' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( \'/\' . $post->post_type . \'/\', \'/\', $post_link );
return $post_link;
}
add_filter( \'post_type_link\', \'wpex_remove_cpt_slug\', 10, 3 );
/**
* Some hackery to have WordPress match postname to any of our public post types
* All of our public post types can have /post-name/ as the slug, so they better be unique across all posts
* Typically core only accounts for posts and pages where the slug is /post-name/
*/
function wpex_parse_request_tricksy( $query ) {
// Only noop the main query
if ( ! $query->is_main_query() ) {
return;
}
// Only noop our very specific rewrite rule match
if ( 2 != count( $query->query ) || ! isset( $query->query[\'page\'] ) ) {
return;
}
// \'name\' will be set if post permalinks are just post_name, otherwise the page rule will match
if ( ! empty( $query->query[\'name\'] ) ) {
$query->set( \'post_type\', array( \'post\', \'YOUR_POST_TYPE_NAME\', \'page\' ) );
}
}
add_action( \'pre_get_posts\', \'wpex_parse_request_tricksy\' );
将“YOUR\\u POST\\u TYPE\\u NAME”更改为您的帖子类型名称。