1

我只希望从首页中排除该类别的最新帖子。应显示同一类别的所有其他人。我似乎无法弄清楚这一点。这是我到目前为止得到的,但它不包括首页的整个类别。

function exclude_category2($query) { 
if ( $query->is_home ) { 
$query->set('cat', '-1,-4,-36'); 
} 
return $query; 
} 
add_filter('pre_get_posts', 'exclude_category2'); 

谢谢您的帮助!

4

1 回答 1

0

You could use some hook like template_redirect to remove the top element from the global $posts array using array_shift like gok suggests (somewhat, he didn't really say how to do it). In that case it would look like this

add_action( 'template_redirect', function() {
    global $posts;
    array_shift( $posts );
});

But I think a more elegant approach would be this

add_action( 'loop_start', function( $args ) {
    $args[0]->next_post();
});

If you put this in functions.php, at the beginning of the Wordpress loop, it will automatically go to the next post which means it will skip the first post always.

If you want to do this only on certain pages, use the relevant template tag like is_home() inside the function. if ( is_home() ) $args[0]->next_post();.

If you are not using PHP version >= 5.3 you will have to give the function a name, since lower versions of PHP do not support anonymous functions.

于 2013-01-23T22:04:55.827 回答