user2719452
user2719452

Reputation: 27

PHP Add every 2 items inside DIV in WordPress loop

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.

Upvotes: 2

Views: 2819

Answers (6)

Raf
Raf

Reputation: 352

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

Upvotes: 0

n00dl3
n00dl3

Reputation: 21574

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>

Upvotes: 2

Gerald Schneider
Gerald Schneider

Reputation: 17797

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.

Upvotes: 2

dingo_d
dingo_d

Reputation: 11680

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.

Upvotes: 0

Pranav Bhatt
Pranav Bhatt

Reputation: 745

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

Upvotes: 0

dev_jonas
dev_jonas

Reputation: 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?

Upvotes: -1

Related Questions