Add div after every three items in a loop?

2019-06-14 13:42发布

I am working with a WP website and in my template I am running the loop like this:

<!-- START LOOP -->
                <?php while ( have_posts() ) : the_post(); ?>

                    <div class="row" style="margin:15px 0;">
                        <div class="twelve columns">
                            <div class="four columns">
                                <a href="<?php the_permalink(); ?>">
                                    <?php 
                                        if ( has_post_thumbnail() ) {
                                            the_post_thumbnail( 'medium' );
                                        } else {
                                            echo 'No Preview Available'; 
                                        } 
                                    ?>
                                </a>
                            </div>
                            <div class="eight columns">

                                <h3><a href="<?php the_permalink(); ?>"><?php the_title() ?></a></h3>
                                <p><?php the_excerpt() ?></p>
                                <p><a href="<?php echo esc_html( get_post_meta( get_the_ID(), 'portfolio_website', true ) ); ?>" target="_blank"><?php echo esc_html( get_post_meta( get_the_ID(), 'portfolio_website', true ) ); ?></a></p>

                            </div>  

                        </div>
                    </div>
                    <hr />

                <?php endwhile; ?>

Because this is a responsive website, I am trying to get a grid column look. The problem I am having is how I can insert a div with a class or "row container" after every third item?

I know I probably just confused the crap out of you because I confuse myself but in short the html would look like this:

<div class="row container">
    <!-- item 1 -->
    <div class="twelve columns">
        <div class="four columns">IMAGE HERE</div>
        <div class="eight columns">TEXT HERE</div>
    </div>

    <!-- item 2 -->
    <div class="twelve columns">
        <div class="four columns">IMAGE HERE</div>
        <div class="eight columns">TEXT HERE</div>
    </div>

    <!-- item 3 -->
    <div class="twelve columns">
        <div class="four columns">IMAGE HERE</div>
        <div class="eight columns">TEXT HERE</div>
    </div>

    <!-- item 4 -->
    <div class="twelve columns">
        <div class="four columns">IMAGE HERE</div>
        <div class="eight columns">TEXT HERE</div>
    </div>
</div>

and so on, instead I would like it to display in a grid so the HTML should look more like this:

<div class="row container">
    <!-- row 1 -->
    <div class="twelve columns">
        <div class="four columns">
            <!-- item 1 -->
            <div class="four columns">IMAGE HERE</div>

            <!-- item 2 -->
            <div class="four columns">IMAGE HERE</div>

            <!-- item 3 -->
            <div class="four columns">IMAGE HERE</div>
        </div>
    </div>

    <!-- row 2 -->
    <div class="twelve columns">
        <div class="four columns">
            <!-- item 4 -->
            <div class="four columns">IMAGE HERE</div>

            <!-- item 5 -->
            <div class="four columns">IMAGE HERE</div>

            <!-- item 6 -->
            <div class="four columns">IMAGE HERE</div>
        </div>
    </div>
</div>

I can do everything else Im just not sure how to implement the below so I get the results I pasted above? I have found this online and feel like it is a step in the right direction:

<?php ($i % 3) == 0 ?>

2条回答
霸刀☆藐视天下
2楼-- · 2019-06-14 14:29

What I needed was a counter:

<!-- START LOOP -->
            <?php $counter = 1 ?>
            <div class="row" style="margin:15px 0;">
                    <div class="twelve columns">
            <?php while ( have_posts() ) : the_post(); ?>


                        <div class="four columns">
                            <a href="<?php the_permalink(); ?>">
                                <?php 
                                    if ( has_post_thumbnail() ) {
                                        the_post_thumbnail( 'medium' );
                                    } else {
                                        echo 'No Preview Available'; 
                                    } 
                                ?>
                            </a>
                        </div>  
                        <?php if ($counter % 3 == 0){echo '</div></div></hr /><div class="row" style="margin:15px 0;"><div class="twelve columns">';} ?>                            


            <?php $counter++ ; 
            endwhile; ?>
            </div>
            </div>
查看更多
ゆ 、 Hurt°
3楼-- · 2019-06-14 14:40

Your feelings are right.

You can use the $current_post property of the WP_Query class to get the index of the post currently displayed in the loop, and then use the modulus operator to make sure you are targeting multiples of three.

So your loop could look like this:

 <div class="row container">
      <!-- row -->
      <div class="twelve columns">
           <div class="four columns">
      <?php while ( have_posts() ) : the_post(); ?>

                <!-- item -->            
                <div class="four columns">IMAGE HERE</div>

      <?php if( $wp_query->current_post % 3 == 0 ) : ?>
           </div>
      </div>
      <!-- row -->
      <div class="twelve columns">
           <div class="four columns">
      <?php endif; ?>        
      <?php endwhile; ?>
</div>

You might need to improve this implementation. Specifically, make sure that, whatever happens, your HTML closes correctly.

查看更多
登录 后发表回答