Wordpress,过滤主页上的消息

时间:2013-08-30 10:11:08

标签: php wordpress

如何使用我网站主页上的某些ID隐藏某些类别的帖子?我需要像过滤器这样的解决方案:

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

    return $query;
}

add_filter('pre_get_posts', 'exclude_post');

有人可以提供一个例子吗?

3 个答案:

答案 0 :(得分:3)

使用$query->set( $query_var, $value );其中$ query_var是您要在查询中添加/更新的变量。所以把它放在你的条件中:

// 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 ) );

记住,对$query->is_main_query()进行检查的条件是好的做法。 pre_get_posts是一个操作挂钩,因此您必须将add_filter更改为add_action。 动作钩子不会返回值,过滤器会返回值。

示例

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');

<强>更新

根据问题出现的新细节,为了排除Feed流中的某些帖子,但不在类别存档中,条件检查可能如下所示:

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

OR

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

希望它有所帮助!

答案 1 :(得分:0)

您还可以使用以下方式从帖子查询中排除某个类别

 <?php 
 if ( is_home() ) 
 {
 query_posts($query_string . '&cat=-3');
 }
 ?>

答案 2 :(得分:-1)

您可以在查询参数中使用简单排除: -

<?php wp_list_categories('orderby=name&show_count=1&exclude=10'); ?> 

它唯一的样本,如何排除。

跳起来......