2

I need PHP code to add every 2 items inside DIV in WordPress loop.

For example, I need like this:

<div class="wrap">
    post
    post
 </div>

<div class="wrap">
    post
    post
 </div>

<div class="wrap">
    post
    post
 </div>

This is my wordpress loop, but not working, I need every 2 posts inside DIV:

<?php if ( have_posts() ) : // If have post start. ?>


    <?php $i = 0; ?>


    <?php while ( have_posts() ) : the_post(); // Start Loop: ?>


        <?php if ( $i % 2 ==  0) : ?>
            <div class="wrap">
        <?php endif; ?>


        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <?php the_content(); ?>
        </article>


        <?php if ( $i % 2 == 0 ) : ?>
            </div>
        <?php endif; ?>


    <?php $i++; endwhile; // End Loop. ?>


<?php endif; // If have post end. ?>

Thanks.

6
  • If you're in the main loop then you can check with something like: if( $wp_query->current_post % 2 == 0 ){...} Or you can set the counter $i=0; outside the loop (if ( have_posts() ) : while ( have_posts() ) : the_post();) and then check if it's modulo 2 is 0: $if( i % 2 == 0) and increment on each iteration ($i++;), before endwhile. Commented Mar 31, 2016 at 10:45
  • not working @dingo_d Commented Mar 31, 2016 at 11:52
  • Show the code that isn't working. Commented Mar 31, 2016 at 11:55
  • @dingo_d Read my wordpress loop code. Commented Mar 31, 2016 at 12:03
  • may this link help you. Commented Mar 31, 2016 at 12:10

6 Answers 6

2

The problem is that you print both <div> and </div> on even values of $i. That's why they always wrap only one and the second post stands aside.

You have to echo the <div> on even numbers and </div> on odd:

<?php if ( have_posts() ) : // If have post start. ?>

    <?php $i = 0; ?>

    <?php while ( have_posts() ) : the_post(); // Start Loop: ?>

        <?php if ( $i % 2 ==  0) : ?>
            <div class="wrap">
        <?php endif; ?>

        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <?php the_content(); ?>
        </article>

        <!-- changed == 0 to != 0  -->
        <?php if ( $i % 2 != 0 ) : ?>
            </div>
        <?php endif; ?>

    <?php $i++; endwhile; // End Loop. ?>

        <!-- added closing </div> for odd number of posts -->
        <?php if ( $i % 2 != 0 ) : ?>
            </div>
        <?php endif; ?>

<?php endif; // If have post end. ?>

I added a second </div> after the loop, because without it you wouldn't get the closing tag at all if you have an odd number of posts.

Sign up to request clarification or add additional context in comments.

1 Comment

Similar to what I placed above :D
2

I think this should do the job:

<div class="wrap">
<?php
$query = new WP_Query();
if ( $query->have_posts() ):
  $i=0;
  while ( $query->have_posts() ) :
    $query->the_post();
    if($i%2==0 && $i<$query->post_count-1 && $i>0):
      echo '</div><div class="wrap">' 
    endif;
?>
    <!--html here-->
<?php
    $i++;
  endwhile;
endif;
?>
</div>

1 Comment

I need default loop, not custom loop wp_query
0

You should do something like this:

    <?php 
    if ( have_posts() ) {
        $i=0;
        while ( have_posts() ) {
           if($i%2==0):?>
         <div class="wrap">
   <?php     
    else : ?>
</div>
<?php 
  endif;       
  $i++;
}
}    
?>

Comments

0

Ok, based on the answer by Aviram here I created this:

$i = 1;
$out = '';
$endingNeeded = false;

if ( have_posts() ) {
    while ( have_posts() ) {

        if ( $i % 2 == 1) {
            $out .= '<div class="wrap">';
            $endingNeeded = true;
        }

        the_post(); // Start Loop:

        $out .= '<article id="post-'. get_the_ID().'" class="'.implode(get_post_class(), ', ').'">
                '.get_the_content().'
            </article>';

        if ( $i % 2 == 0 ) {
            $out .= '</div>';
            $endingNeeded = false;
        }

        $i++;
    }
}

if($endingNeeded) {
    $out .= '</div>';
}

echo $out;

Should be working fine.

Comments

0

Correct loop should look like this:

 <?php if(have_posts()) : ?>
 <?php $no_of_posts = wp_count_posts()->publish; ?>
 <div class="wrap">
     <?php $i=1; while(have_posts()) : the_post(); ?>
     <div class="post">
         post text
     </div>
     <?php if($i%2==0 && $i!=$no_of_posts) : ?>
     </div>
     <div class="wrap">
     <?php endif; ?>
     <?php $i++; endwhile; ?>
 </div>
 <?php endif; ?>

Comments

-1

You meaning this like?

<?php 
if ( have_posts() ) {
    while ( have_posts() ) {
        for ($i=0; $i<2; $i++) {
            // what do you like to do
            ?>
            <!-- HTML CODE -->
            <?php
        }
    } 
}else{
    // ...
}
?>

Why do you not add a simple for loop in the WordPress loop?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.