【问题标题】:Show posts in random order from category loop从类别循环中以随机顺序显示帖子
【发布时间】:2013-09-07 16:27:43
【问题描述】:

我在 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><!-- 
                   -->";

    } 
  } 
} 
?>  

更新:在 iEmanuele 的建议下解决 - 添加 shuffle($categories);

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    使用shuffle( $array ); 打乱数组中的元素。

    $categories = get_categories();
    shuffle( $categories );
    
    foreach( $categories as $category ){
        //Your stuff
    }
    

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多