【问题标题】:hide custom post type on archives page在档案页面上隐藏自定义帖子类型
【发布时间】:2019-03-22 15:44:11
【问题描述】:

我有一个 wordpress 存档页面,其中显示了我创建的自定义帖子类型,我实际上希望隐藏它。

我使用插件 CPT UI 来创建帖子类型“事件”

在我的博客上,我有一个标记为“精选”的类别 (website.com/category/featured/),在这个“精选”类别页面上,我显示了一些我不想显示的事件 CPT。

我在我的 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;
}

想法??

【问题讨论】:

    标签: php wordpress custom-post-type archive


    【解决方案1】:

    $query->set( 'post_type', array('event') ); 您并未排除事件。你包括它。

    要排除事件,您必须传递您拥有并想要显示的所有帖子类型,“事件”除外

    像这样。

    如果您没有任何自定义帖子类型。

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

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

    所以你的代码应该是这样的

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-06-04
      • 2017-11-21
      • 1970-01-01
      • 1970-01-01
      • 2016-06-05
      • 2018-08-29
      • 2017-06-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多