如何在 WP 上显示随机帖子,最后一个帖子除外
问题描述:
我找到了部分答案,我是 Wordpress 的新手.我希望在主页上显示随机帖子,但最后一个帖子除外我找到了这个
I was able to locate part of the answer, I'm new to Wordpress. Im looking to show random post on the main page, but except the last post i found this
add_action('pre_get_posts', 'my_pre_get_posts');
function my_pre_get_posts($query) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set('orderby', 'rand');
}
}
提前致谢
答
这里的代码请在functions.php文件中使用此代码并输入您需要排除该帖子的最后一个帖子ID.
Here is the code please use this code in functions.php file and enter the last post id which you need to exclude the post.
add_action('pre_get_posts','my_pre_get_posts');
function my_pre_get_posts($query) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set('orderby', 'rand');
$query->set('post__not_in',array(1));
}
}
请使用它,我也在搜索最好的解决方案,您无需输入静态最后一个帖子 id =1 .
谢谢你.
Please use it and I 'm also searching a best possible solution in which you need not to enter the static last post id =1 .
Thanking you.