0

我的 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>  
4

1 回答 1

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 } ?>
于 2013-07-31T06:38:43.370 回答