Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want 3 post with one loop iteration, so I'm using the_post() to achieve this: (html removed)

<?php while ( have_posts() ) : the_post();

    the_title();
    the_post();
    the_title();
    the_post();
    the_title();

endwhile; ?>

The problem starts, if number of posts is not divisible by 3. For example, if I have 5 posts, than this loop will still try to display 6 of them, generating empty html. How can I work it out? Or maybe I'm trying to achieve this functionality the wrong way?

@edit: Here is HTML I wanna output to clarify my question:

<div class="row">
<div class="span3">
    <h2>Title</h2>
    <p>Content</p>
</div>
<div class="span3">
    <h2>Title</h2>
    <p>Content</p>
</div>
<div class="span3">
    <h2>Title</h2>
    <p>Content</p>
</div>
 </div>

 <div class="row">
<div class="span3">
    <h2>Title</h2>
    <p>Content</p>
</div>
<div class="span3">
    <h2>Title</h2>
    <p>Content</p>
</div>
<div class="span3">
    <h2>Title</h2>
    <p>Content</p>
</div>
</div>

So I want to surround every 3 posts with .row.

share|improve this question
1  
In human terms, what are you trying to accomplish? Note: The the_post() template tag doesn't actually output the post; rather, it merely sets up the $post global, and all of the template tags that rely on the $post global (such as the_title()). So, it should only ever be called once inside the Loop. –  Chip Bennett Aug 25 '12 at 16:25
    
@ChipBennett: I've posted clarification in original question. I know, that the_post() doesn't output anything, I'm just trying to find a hack for my problem. –  smogg Aug 25 '12 at 16:31
    
RE: Your post edit: that's code. I'm looking for human terms to describe what you're trying to accomplish. Code is an implementation; whether that implementation is the correct approach or not depends on what you're ultimately trying to accomplish. For example: are you trying to display a grid of posts from the same query, or perhaps display three posts from one query, followed by three posts from a different query (such as set of featured posts, followed by a different set of featured posts)? –  Chip Bennett Aug 25 '12 at 16:32
    
@ChipBennett One, custom post type query, I want to display them in a grid. –  smogg Aug 25 '12 at 16:34
1  
Instead of calling the_post three times, just add a counter to do whatever it is that you want to do to separate the three posts and stick it behind a modulo comparison. if ($counter % 3 == 0) do stuff sort of thing. –  Otto Aug 25 '12 at 16:48

2 Answers 2

up vote 0 down vote accepted

Ultimately, this question is off-topic, because the solution has nothing to do with WordPress. Outputting posts in a grid pattern is an HTML/CSS issue, and the best-practice solutions don't involve putting every three posts inside a separate containing <div>. Even so, you can do what you want to do, using PHP (which, technically speaking, still makes this question off-topic).

I would start by querying a number of posts divisible by 3, or else setting posts_per_page to a number divisible by 3. How you do that depends on whether you're calling a new WP_Query() for your custom-post type loop, or if you're filtering pre_get_posts to modify the default query. Either way, you would use the posts_per_page query argument.

Then, you can simply define a counter inside the loop, and use it to close/open DIVs as necessary:

<div class="row">
<?php
$counter = 1;
if ( have_posts() ) : while ( have_posts() ) : the_post();

    global $wp_query;
    the_title(); 
    the_content(); 
    if ( ( 0 == $counter % 3 ) && ( $counter != $wp_query->posts_per_page ) ) {
    ?>
</div><div class="row">
    <?php
    }
    $counter++;
endwhile; endif;
?>
</div>

The second part of the conditional ensures that you don't get an empty div after the last row.

share|improve this answer

After posting this question, I've had the "eureka" moment and achieved it (probably the ugly way) by surrounding my html for every post with simple if(get_the_title()). So if title doesn't exist, than don't display anything. But I still think there is more pretty way of doing this.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.