从类别循环以随机顺序显示帖子

时间:2013-09-07 16:27:43

标签: php wordpress

我在Wordpress中有一个类别循环,它根据类别输出不同的标记。这一切都很好,现在我需要帖子以随机顺序显示。我用'orderby => “rand”但这只是随机化每个类别中的帖子,即类别本身仍按时间顺序输出。我不确定该怎么做,并希望得到任何帮助。

代码:

<?php

$categories = get_categories();
  foreach($categories as $category) {
  $args=array(
  'category__in' => array($category->term_id),
  'caller_get_posts'=>1,
  'orderby' => 'rand'
);
$posts=get_posts($args);
  shuffle($posts);
  if ($posts) {
    foreach($posts as $post) {
      setup_postdata($post); 
                   echo "<li class='cat-{$category->term_id}'><a href='".get_permalink()."'>".get_the_post_thumbnail()."</a></li><!-- 
                   -->";

    } 
  } 
} 
?>  

更新:已解决withiEueuele的建议 - 添加随机播放($ categories);

1 个答案:

答案 0 :(得分:4)

使用shuffle( $array );随机播放数组中的元素。

$categories = get_categories();
shuffle( $categories );

foreach( $categories as $category ){
    //Your stuff
}

希望它有所帮助!