[英]How do I exclude only the latest post from a certain category to display on the front page on WordPress?
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 using array_shift
like gok suggests (somewhat, he didn't really say how to do it). 您可以使用类似
template_redirect
钩子,通过gok建议的array_shift
从全局$ posts数组中删除顶部元素(有点,他没有真正说出如何做)。 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. 如果将其放在functions.php中,则在Wordpress循环开始时,它将自动转到下一篇文章,这意味着它将始终跳过第一篇文章。
If you want to do this only on certain pages, use the relevant template tag like is_home()
inside the function. 如果只想在某些页面上执行此操作,请在函数内部使用相关的模板标记,例如
is_home()
。 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. 如果您未使用PHP版本> = 5.3,则必须给该函数命名,因为较低版本的PHP不支持匿名函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.