简体   繁体   English

隐藏档案页面上的自定义帖子类型

[英]hide custom post type on archives page

I have a wordpress archives page that is displaying a custom post type that I've created that I actually want hidden. 我有一个wordpress存档页面,该页面显示了我实际上想要隐藏的自定义帖子类型。

I used the plugin, CPT UI, to create the post type "Event" 我使用了插件CPT UI创建帖子类型“事件”

On my blog, I have a category labeled Featured (website.com/category/featured/), and on this Featured category page I have a few Event CPT's showing up that I want to not be displaying. 在我的博客上,我有一个标记为“特色”的类别(website.com/category/featured/),在此“特色类别”页面上,有一些我不想显示的事件CPT。

I've tried the following code within my functions.php file that did not work: 我已经在我的functions.php文件中尝试了以下无效的代码:

add_action( 'pre_get_posts', 'exclude_cpt' );
function exclude_cpt( $query ) {
    if ( $query->is_category('featured') ) {
        $query->set( 'post_type', array('event') );
    }
    return $query;
}

Thoughts?? 思考?

By $query->set( 'post_type', array('event') ); 通过$query->set( 'post_type', array('event') ); you are not excluding the Events. 您并不排除活动。 you are including it. 你包括在内。

To exclude events you have to pass all post type you have and want to show, except 'event' 要排除事件,您必须传递所有您想显示的帖子类型,“ event”除外

like this. 像这样。

if you don't have any custom post type. 如果您没有任何自定义帖子类型。

$query->set('post_type', array( 'post', 'page' ) );

or 要么

$query->set('post_type', array( 'post', 'page', 'post_type_1', 'post_type_2' ) );

so your code should look like this 所以你的代码应该看起来像这样

add_action( 'pre_get_posts', 'exclude_cpt' );
function exclude_cpt( $query ) {
    if ( $query->is_category('featured') ) {
        $query->set( 'post_type', array( 'post' ) ); // this will display only posts and pages
    }
    return $query;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM