我在分类档案(艺术家)中使用基本循环代码,我想知道如何设置循环以随机顺序显示帖子(' orderby' =>&# 39; rand')当我添加数组时,它似乎无法工作?任何帮助都会很棒!
<?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;
?>
答案 0 :(得分:6)
<?php
$query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '-1' ) );
if( $query->have_posts() ):
// Start the Loop.
while ( $query->have_posts() ) : $query->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.
*/
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;
?>
答案 1 :(得分:0)
query_posts(阵列( &#39; showposts&#39; =&GT; 6, &#39;的OrderBy&#39; =&GT; &#39;兰特&#39 ;, &#39; CATEGORY_NAME&#39; =&GT; &#39;新闻&#39; //您可以插入任何类别名称 ));
答案 2 :(得分:-1)
试试这个:
<?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;
&GT;
答案 3 :(得分:-1)
好问题!
你可以用简单的PHP函数来做到这一点。 http://www.php.net/manual/en/function.shuffle.php
按照以下步骤操作:
如果有任何疑问,请在实施后询问我。
谢谢!
答案 4 :(得分:-2)
你有两种方法可以做到这一点。第一种方式不是最佳方式,但您可能更容易理解:
使用WP_Query
<?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;
在这里,我们将使用自定义WP_Query
对象和orderby
来获取随机帖子。
使用pre_get_posts
最好的方法是使用pre_get_post
操作自动修改页面输出。您可能需要更多编码。