1

I'm having a problem with the WordPress posts loop. I'd like to break down the post loop, to add some HTML div tags in the post. For example, I'm trying to add a div .wrap around the posts after first post. As it's to wrap the posts, so it should be only once.

The problem is that the .wrap div is being repeated because of the loop. How can I break the loop, and add the .wrap div only once and then continue the loop?

Here's my code:

while ($wp_query->have_posts()) :

    $wp_query->the_post(); 
    if($wp_query->current_post <= 1){ ?>
        <div class="x">
        .. post 1
        </div>
    <?php }
    if($wp_query->current_post > 1){ ?>
        <div class="wrap"> <!-- should be only once -->
            <div class="y">
            .. post 2
            </div>

            <div class="y">
            .. post 3
            </div>
        </div><!-- should be only once -->
    <?php } ?>

endwhile;
2
  • you don't have to break the loop Commented Oct 29, 2012 at 11:52
  • Yeah, the wrap output code doesn't loop. Commented Oct 29, 2012 at 11:53

3 Answers 3

3

Try this

<?php
while ($wp_query->have_posts()) :
    $wp_query->the_post(); 
    if($wp_query->current_post <= 1){ ?>
        <div class="x">
        .. post 1
        </div>
        <div class="wrap"> <!-- should be only once -->
    <?php 
    $is_wrapped = 1;
    }
    if($wp_query->current_post > 1){ ?>
        <div class="y">
          .. post 2, then post 3, ...
        </div>
    <?php } 
endwhile;?>
<?php if (!empty($is_wrapped)){ ?>
        </div><!-- should be only once -->
<?php } ?>
Sign up to request clarification or add additional context in comments.

Comments

1

You can just use an iterator index, like this:

$i = 0;
while (...) {
    if ($i == 1) {
       // actions you need to do only once
    }

    ...
    $i++
}

Comments

1
<div class="x">

while ($wp_query->have_posts()) :
    $wp_query->the_post();
    <--print post-->
    if($wp_query->current_post <= 1){
        </div><div class="y">
    }
endwhile;

</div>

You will get an empty <div class="x" /> if there's no post and an empty <div class="y" /> if there's only one post. But may be that's feasible in your case...


edit: example

<?php
$wp_query = new WPDummy; 

echo '<div class="x">';    
while ($wp_query->have_posts()) :
    $wp_query->the_post();
    $wp_query->the_content();
    if($wp_query->current_post <= 1){
        echo '</div>', "\n",'<div class="y">';
    }
endwhile;
echo '</div>';

// boilerplate
class WPDummy {
    public $current = null;
    public $current_post = 0;
    public function __construct() {
        $this->posts = array( array('content'=>'A'),array('content'=>'B'),array('content'=>'C'),array('content'=>'D') );
    }

    public function have_posts() {
        return false!=current($this->posts);
    }

    public function the_post() {
        $this->current_post+=1;
        $this->current = current($this->posts);
        next($this->posts);
    }

    public function the_content() {
        echo '<span class="content">', $this->current['content'], '</span>';
    }
}

prints

<div class="x"><span class="content">A</span></div>
<div class="y"><span class="content">B</span><span class="content">C</span><span class="content">D</span></div>

1 Comment

my question is to add an additional div wrap inside the loop only once.

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.