dpqaaczn141761 2014-07-17 00:43
浏览 6
已采纳

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

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

  • 写回答

1条回答 默认 最新

  • dtlh12053 2014-07-17 00:49
    关注

    Use PHP's MOD to test for a remainder:

    $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; 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题