使用下面的代码,我会在帖子末尾随机显示5个帖子。如您所见,如果当前帖子(ID)是随机选择的帖子之一,则它不会显示。
这意味着我要展示的是5个帖子而不是5个帖子。在其他情况下,会有5个帖子。
我的问题是如何编辑下面的代码,只显示5个帖子,即使当前帖子是随机选择的帖子之一。
<?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();
?>
答案 0 :(得分:0)
选择预期的1个额外帖子,只有在必须跳过一个时才显示。
CNC中 这非常难看,但应该有效:
<?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();
?>