Php WordPress循环中的随机顺序?
我在分类法存档(artists)中使用基本循环代码,我想知道如何设置循环以随机顺序显示帖子('orderby'=>'rand')。当我添加数组时,它似乎不起作用?任何帮助都会很好Php WordPress循环中的随机顺序?,php,wordpress,loops,Php,Wordpress,Loops,我在分类法存档(artists)中使用基本循环代码,我想知道如何设置循环以随机顺序显示帖子('orderby'=>'rand')。当我添加数组时,它似乎不起作用?任何帮助都会很好 <?php // Start the Loop. while ( have_posts() ) : the_post(); /* * Include
<?php
// Start the Loop.
while ( have_posts() ) : the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
array ( 'orderby' => 'RAND' );
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next page navigation.
twentyfourteen_paging_nav();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
?>
试试这个:
<?php
$args = array(
'orderby' => 'rand'
);
$query = new WP_Query($args);
if (have_posts()) {
while ( $query->have_posts() ) : $query->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next page navigation.
twentyfourteen_paging_nav();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
先问个好问题
您可以使用PHP的简单函数来实现这一点。
遵循以下步骤:
第一件事是通过查询获取所有帖子
您知道wordpress将以数组格式提供结果。因此,不要尝试更多的编码,因为它太复杂了
现在您有了结果数组,所以只需使用PHP函数shuffle。
如果有任何疑问,请在实施后询问我
谢谢 有两种方法。第一种方法不是最好的方法,但您可能更容易理解:
使用WP\u查询
<?php
$args = array(
'orderby' => 'random'
);
$query = new WP_Query( $args );
if( $query->have_posts() ):
// Start the Loop.
while ( $query->have_posts() ) : $query->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next page navigation.
twentyfourteen_paging_nav();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
查询帖子(数组)(
“展示柱”=>6,
'orderby'=>'rand',
'category_name'=>'News'//您可以插入任何类别名称
)); 这不适用于OP的要求。你应该在($query->have_posts()):$query->the_post()时执行代码>处理自定义wP\u查询对象时。简单而干净的解决方案!请注意,循环可能位于不同的文件(例如index.php、home.php、template-centered.php等)中。非常感谢。如果您希望将每个类别中的所有帖子随机化,那么您可以使用add pre_get_post函数添加到functions.php,如下所示:add_action('pre_get_posts','randomise');函数randomise($query){$query->set('orderby','rand');}
<?php
$args = array(
'orderby' => 'random'
);
$query = new WP_Query( $args );
if( $query->have_posts() ):
// Start the Loop.
while ( $query->have_posts() ) : $query->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next page navigation.
twentyfourteen_paging_nav();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;