duanqiang5722 2011-09-18 19:48
浏览 46
已采纳

即使我没有显示当前帖子,如果随机选择,也总是显示5个帖子

With the code below, I show 5 posts ordered randomly at the end of a post. As you can see, if the current post (ID) is one of the random chosen posts then it doesn't show up.

That means instead of the 5 posts I want to show there are 4. In other case, there will be 5 posts.

My question is how to edit the code below to show only 5 posts even if the current post is one of the randomly chosen.

<?php
query_posts( 'posts_per_page=5&orderby=rand' );

while (have_posts()) : the_post();
    if ( $post->ID == $ID  ) continue;
the_title();
endwhile;
    wp_reset_query(); 
?>
  • 写回答

1条回答 默认 最新

  • doujiao3346 2011-09-18 19:52
    关注

    Select 1 extra post anticipated and only show it if you had to skip one.

    -edit- This is very ugly but it should work:

    <?php
        query_posts( 'posts_per_page=6&orderby=rand' );
        $counter = 0;
        while (have_posts()) : the_post();
            if ( $post->ID == $ID  || $counter == 5 ) continue;
            $counter++;
            the_title();
        endwhile;
        wp_reset_query(); 
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?