简体   繁体   English

Wordpress和Bootstrap 3自动生成类别中的行

[英]Wordpress and Bootstrap 3 auto generate row in categories

I'm using bootstrap 3 and I would like to group 3 post categories per row to get a proper grid system but I don't know how to make a counter. 我正在使用bootstrap 3,我想每行将3个帖子类别分组以获得一个正确的网格系统,但是我不知道如何进行计数。 Can anybody help me with this? 有人可以帮我吗?

Here is my code: 这是我的代码:

<?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; ?>

I would like to have something like this 我想要这样的东西

<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>

Thank you very much for your help. 非常感谢您的帮助。

Use PHP's MOD to test for a remainder: 使用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; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM