如何在 WP 上显示随机帖子,最后一个帖子除外 [英] How to Show random post on WP, except the last post

查看:19
本文介绍了如何在 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.

这篇关于如何在 WP 上显示随机帖子,最后一个帖子除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆