【发布时间】:2015-02-12 18:37:16
【问题描述】:
我认为我非常接近解决方案,但我无法弄清楚我应该在哪里关闭最后一个 div。
我有一个 wordpress 查询请求四个帖子。
我想将<section class="sponsor-row">中的每两个项目包装起来
所以我在我的查询中实现了一个计数器来计算每个帖子,如果它发现两个帖子已经输出,它会关闭<section> div。
但是我不知道如果该行应该再次关闭,我应该如何解决。
任何人都可以解决这个问题吗?如何让它在<section class="sponsor-row"> 中包装每两个输出?
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$counter = 0;
echo '<section class="sponsor-row">'; // Opening the section here
if ( has_post_thumbnail() ) {
$counter++;
echo '<div class="grid half">';
echo '<a class="sponsor" href="'.get_the_permalink().'" target="_blank">';
the_post_thumbnail( 'full' );
echo '</a>';
echo '</div>';
if ($counter <= 2){
echo '</section>'; // closing the section if two posts
$counter = 0;
}
}
}
}
【问题讨论】:
-
您只想将每个页面的前两个帖子包含在
.sponsor-row中还是前两个和后两个? -
你能告诉我们最终输出是什么吗?