dqhgjay5753 2013-08-30 10:11
浏览 6
已采纳

Wordpress,过滤主页上的消息

How can I hide posts from some category with some ID from main page of my site? I need solution like filter:

function exclude_post($query) {
    if ($query->is_home) {
        ...
    }

    return $query;
}

add_filter('pre_get_posts', 'exclude_post');

Can somebody provide an example?

  • 写回答

3条回答 默认 最新

  • dongwen7423 2013-08-30 10:31
    关注

    Use $query->set( $query_var, $value ); where $query_var is the variable you want to add/update in query. So put this inside your condition:

    // 1st parameter is the query variable the 2nd is its value, in this case an array of category IDs
    $query->set( 'category__not_in', array( 2, 6 ) );
    

    Remember is good practice put in condition a check to $query->is_main_query(). pre_get_posts is an action hook so you have to change add_filter to add_action. An action hook doesn't return a value, a filter does.

    Example

    function exclude_post( $query ) {
        if ( $query->is_home() && $query->is_main_query() ) {
          $query->set( 'category__not_in', array( 2, 6 ) );
        }
    }
    
    add_action('pre_get_posts', 'exclude_post');
    

    UPDATE

    According to the new details emerging by the question,in order to exclude some posts in feeds stream, but not in category archive, the conditional check might look like:

    if( $query->is_feed() && !$query->is_archive() )
    

    OR

    if( $query->is_feed() && !$query->is_category() )
    

    Hope it helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向