将div添加到循环中的每一行

时间:2013-09-26 22:38:55

标签: php css wordpress twitter-bootstrap

我正在尝试查询要在WordPress网站主页上显示的帖子图片。

我希望最终结果看起来像这样   - enter image description here

我可以正确显示跨度,但我不知道如何将Bootstrap的'row'类添加到每一行。

这是我到目前为止所拥有的 -

感谢任何帮助。

谢谢!

$args = array( 'post_type' => 'video', 'posts_per_page' => 10,);

$the_query = new WP_Query( $args );

echo '<section id="our-work">';

if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();

     $counter += 1;

     if($counter == 4 || $counter == 5 || $counter == 9 || $counter == 10) : 

     echo '<div class="span6">';
     the_post_thumbnail();;
     echo '</div>';

     else:

     echo '<div class="span4">';
     the_post_thumbnail();
     echo '</div>';

     endif;


 endwhile; endif;

echo '</section>';

2 个答案:

答案 0 :(得分:4)

肯定有更优雅的方式来做到这一点......但这应该有用。

$args = array( 'post_type' => 'video', 'posts_per_page' => 10,);

$the_query = new WP_Query( $args );

echo '<section id="our-work">';

if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
    $counter += 1;

    if ($counter == 1 || $counter == 4 || $counter == 6 || $counter == 9) {
      echo '<div class="row">';
    }

    if($counter == 4 || $counter == 5 || $counter == 9 || $counter == 10) : 

    echo '<div class="span6">';
    the_post_thumbnail();
    echo '</div>';

    else:

    echo '<div class="span4">';
    the_post_thumbnail();
    echo '</div>';

    endif;

    if ($counter == 3 || $counter == 5 || $counter == 8 || $counter == 10) {
      echo '</div>';
    }

 endwhile; endif;

echo '</section>';

答案 1 :(得分:2)

毫无疑问,它可以用不同的方式编写 - 或者更好,但这应该可以解决问题:)

if($counter % 3 == 0) {
    $current_class = "span6";
} else {
    $current_class = "span4";
}

// your stuff here
echo '<div class="'.$current_class.'">';