dongyan2445 2018-10-17 21:16
浏览 79

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

I have a wordpress archives page that is displaying a custom post type that I've created that I actually want hidden.

I used the plugin, CPT UI, to create the post type "Event"

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.

I've tried the following code within my functions.php file that did not work:

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??

  • 写回答

1条回答 默认 最新

  • douling6469 2018-10-17 21:43
    关注

    By $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'

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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效