Wordpress相关帖子仅显示该类别中的最后三个帖子。有没有办法随机化每个相关的帖子?

时间:2013-07-31 05:37:58

标签: javascript wordpress

我的wordpress相关帖子仅显示每个类别中最近创建的最近三个帖子。我希望它能在每个类别中显示随机帖子。因此,甚至可以查看较旧的帖子。不只是最后三个创建。这是下面的代码。

<?php
$categories = get_the_category($post->ID);
if ($categories) {
$category_ids = array();
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;

$args=array(
'category__in' => $category_ids,
'post__not_in' => array($post->ID),
'showposts'=>3, // Number of related posts that will be shown.
'caller_get_posts'=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
    echo '<ul>';
    while ($my_query->have_posts()) {
        $my_query->the_post();
    ?>
<div class="relatedpost">  
    <a rel="external" href="<? the_permalink()?>"><?php the_post_thumbnail(array(175,98)); ?><br />  
<center><h6><?php the_title(); ?></h6></center> 
    </a>  
</div>  

1 个答案:

答案 0 :(得分:2)

参数中添加 orderby rand 并使用如下所示

<?php 
   $posts = get_posts('orderby=rand&numberposts=5'); 
   foreach($posts as $post) { ?>
   <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
<?php } ?>