Php 如何在WP上显示随机帖子,最后一篇帖子除外
我找到了部分答案,我是Wordpress的新手。 我期待在主页上显示随机文章,但除了最后一篇文章 我找到了这个Php 如何在WP上显示随机帖子,最后一篇帖子除外,php,wordpress,Php,Wordpress,我找到了部分答案,我是Wordpress的新手。 我期待在主页上显示随机文章,但除了最后一篇文章 我找到了这个 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', 'ra
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。
add_action('pre_get_posts','my_pre_get_posts');
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));
}
}
函数my_pre_get_posts($query){
如果($query->is_home()&&&&$query->is_main_query()){
$query->set('orderby','rand');
$query set('post__not in',array(1));
请使用它,我还在寻找一个最好的解决方案,您不需要输入静态的最后一篇文章id=1。谢谢。所说的“最后一篇文章”是指最后一篇发表的文章吗?wordpress的问题应该被认为是在最后一篇发表的文章中发表的,对不起:)