我有下面的代码,它得到了前3篇文章。然而,我希望用不同的if statements
. 但当我加上if statements
(为了确定是第一个帖子还是第二个帖子),它们被添加到了前3个帖子的顶部:意外创建了5个帖子。
有没有合适的方式来设计帖子的风格,取决于它们是第一、第二和第三,但要保持在showposts => 3
属性
<?php
$lifestyle_cat = array(
\'category_name\' => \'lifestyle\',
\'showposts\' => 3,
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'orderby\' => \'date\',
\'order\' => \'DESC\'
);
$lifestyle_query = new WP_Query( $lifestyle_cat );
if ( $lifestyle_query->have_posts()) {
$i = 0;
while ( $lifestyle_query->have_posts()) {
$i++;
$lifestyle_query->the_post();
if ($i==1) { // these are added and not within the 3 show posts
?> <p> <?php the_title();?> i = 1</p> <?php
} elseif ($i==2) { // these are added and not within the 3 show posts
?> <p> <?php the_title();?> i = 2</p> <?php
} ?>
<p><?php the_title(); ?></p>
<?php } } else {
// no posts found
}
wp_reset_postdata(); ?>
最合适的回答,由SO网友:Rajeev Vyas 整理而成
你只需要再来一个else
声明,就是这样。
<?php
$lifestyle_cat = array(
\'category_name\' => \'lifestyle\',
\'showposts\' => 3,
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'orderby\' => \'date\',
\'order\' => \'DESC\'
);
$lifestyle_query = new WP_Query( $lifestyle_cat );
if ( $lifestyle_query->have_posts()) {
$i = 0;
while ( $lifestyle_query->have_posts()) {
$i++;
$lifestyle_query->the_post();
if ($i==1) { // these are added and not within the 3 show posts
?> <p> <?php the_title();?> i = 1</p> <?php
} elseif ($i==2) { // these are added and not within the 3 show posts
?> <p> <?php the_title();?> i = 2</p> <?php
} else { ?>
<p><?php the_title(); ?></p>
<?php } ?>
<?php } } else {
// no posts found
}
wp_reset_postdata(); ?>