I only want the latest post from the category to be excluded from the front page. All others from the same category should be displayed. I can’t seem to figure this one out. Here’s what I got so far but it excludes the entire category from the front page.
function exclude_category2($query) {
if ( $query->is_home ) {
$query->set('cat', '-1,-4,-36');
}
return $query;
}
add_filter('pre_get_posts', 'exclude_category2');
Thanks for the help!
You could use some hook like
template_redirect
to remove the top element from the global $posts array usingarray_shift
like gok suggests (somewhat, he didn’t really say how to do it). In that case it would look like thisBut I think a more elegant approach would be this
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.