我正在使用bootstrap 3,我想将每行3个帖子类别分组以获得正确的网格系统,但我不知道如何制作计数器。有人可以帮我吗?
这是我的代码:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo '<div class="col-md-4">';
echo '<a href="';
echo the_permalink();
echo '">';
echo wp_get_attachment_image( $attachment->ID, 'full' );
echo '</a>';
echo '<h3 class="category-title"><a href="';
echo the_permalink();
echo '">';
echo the_title();
echo '</a></h3>';
echo '</div>';
}
}
endwhile; endif; ?>
我想有类似的东西
<div class="row">
<div class="col-md-4">Content</div>
<div class="col-md-4">Content</div>
<div class="col-md-4">Content</div>
</div>
<div class="row">
<div class="col-md-4">Content</div>
<div class="col-md-4">Content</div>
<div class="col-md-4">Content</div>
</div>
非常感谢你的帮助。
答案 0 :(得分:0)
使用PHP的MOD测试余数:
$i = 0;
if ( have_posts() ) : while ( have_posts() ) : the_post();
$i++;
if($i%3 == 1){echo '<div class="row">'; }
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo '<div class="col-md-4">';
echo '<a href="';
echo the_permalink();
echo '">';
echo wp_get_attachment_image( $attachment->ID, 'full' );
echo '</a>';
echo '<h3 class="category-title"><a href="';
echo the_permalink();
echo '">';
echo the_title();
echo '</a></h3>';
echo '</div>';
}
}
if($i%3 == 0){echo '</div>';}
endwhile; endif;