嗨我是相当新的PHP,我有问题添加一个类到一个循环中的三个项目组。所以基本上我需要循环中的项目1-3没有类,项目4-6有类,项目7-9没有类,项目10-12没有类,等等。我知道我需要添加一个计数器,但我不知道如何编写if语句。如何将类添加到循环php中的某些项目?
任何帮助表示赞赏。提前致谢!
对不起这里是代码
global $wp_query;
$wp_query = new WP_Query($args);
if($wp_query->have_posts()):
while($wp_query->have_posts()): $wp_query->the_post(); global $post;
echo '<article class="recipe">
<div class="recipe-img"><a href="'.get_permalink().'">'.get_the_post_thumbnail($id, 'featured').'</a></div>
<div class="recipe-info">
<span>'.get_the_date().'</span> | <span>'.get_the_author().'</span>
<a href="'.get_permalink().'">
<h3 class="lato">'.get_the_title().'</h3>
</a>
</div>
</article>';
}
endwhile;
genesis_posts_nav();
endif;
我能够做到这一点的一个例子,但它只会增加该类每一个第三个项目。
$args = array(
'post_type' => 'recipes',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page'=> '3',
'paged' => get_query_var('paged'),
);
global $wp_query;
$wp_query = new WP_Query($args);
if($wp_query->have_posts()):
$count = 0;
while($wp_query->have_posts()): $wp_query->the_post(); global $post;
$count++;
if ($count % 3 == 0) {
echo '<article class="recipe third">
<div class="recipe-img"><a href="'.get_permalink().'">'.get_the_post_thumbnail($id, 'featured').'</a></div>
<div class="recipe-info">
<span>'.get_the_date().'</span> | <span>'.get_the_author().'</span>
<a href="'.get_permalink().'">
<h3 class="lato">'.get_the_title().'</h3>
</a>
</div>
</article>';
} else {
echo '<article class="recipe">
<div class="recipe-img"><a href="'.get_permalink().'">'.get_the_post_thumbnail($id, 'featured').'</a></div>
<div class="recipe-info">
<span>'.get_the_date().'</span> | <span>'.get_the_author().'</span>
<a href="'.get_permalink().'">
<h3 class="lato">'.get_the_title().'</h3>
</a>
</div>
</article>';
}
endwhile;
genesis_posts_nav();
endif;
哇哦,一个代码示例真的是很必要的。即使你可以用另一种语言编写你需要的东西。 – Iwnnay
请阅读[我可以问哪些主题](http://stackoverflow.com/help/on-topic) 和[如何提出一个好问题](http://stackoverflow.com/help/how-to - 问) 和[完美的问题](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) – RiggsFolly
@Iwnnay我已更新我的问题与代码示例。 – Ozzigity