0

In my query, I'm gathering 6 posts and displaying them into an unordered list. Here's the code I'm using:

$args = array(
    'post_parent' => $page_id->ID,
    'showposts' => 6,
    'post_type' => 'page',
    'orderby' => 'menu_order',
    'order' => 'ASC'
);
$subpages = new WP_query($args);

if ($subpages->have_posts()) :
    while ($subpages->have_posts()) : $subpages->the_post();
        $output .= '<li><a href="'.get_permalink().'" class="'.$page_id->post_name.'-link"><div class="'.$page_id->post_name.'-info"><div class="'.$page_id->post_name.'-img">'.get_the_post_thumbnail($page->ID,'icons').'</div><h2>'.get_the_title().'</h2>
                    <p class="'.$page_id->post_name.'-excerpt">'.get_the_excerpt().'</p>
                    </div></a></li>';
    endwhile;
endif;

I'd like to breakup the and show three posts, then insert a different div, then continue the loop. The html outcome would look similar to:

<ul>
    <li>Post1</li>
    <li>Post2</li>
    <li>Post3</li>
</ul>
<div class="notice">Some text goes here</div>
<ul>
    <li>Post4</li>
    <li>Post5</li>
    <li>Post6</li>
</ul>

EDIT I got this working with the suggestions from @Rarst. Here's my updated code:

    $args = array(
        'post_parent' => $page_id->ID,
        'showposts' => 6,
        'post_type' => 'page',
        'orderby' => 'menu_order',
        'order' => 'ASC'
    );
    $subpages = new WP_query($args);

    if ($subpages->have_posts()) :
        while ($subpages->have_posts()) : $subpages->the_post();
            $output .= '<li><a href="'.get_permalink().'" class="'.$page_id->post_name.'-link"><div class="'.$page_id->post_name.'-info"><div class="'.$page_id->post_name.'-img">'.get_the_post_thumbnail($page->ID,'icons').'</div><h2>'.get_the_title().'</h2>
                        <p class="'.$page_id->post_name.'-excerpt">'.get_the_excerpt().'</p>
                        </div></a></li>';
            if (2 == $subpages->current_post):
                    $output .= '</ul>';
                    $output .= 'test';
                    $output .= '<ul class="'.$page_id->post_name.'">';
            endif;
        endwhile;
    endif;
1

Instance of WP_Query tracks number of current post in current_post property, zero based.

So inside your loop you can check for something like if ( 2 == $subpages->current_post ) and output your additional content.

1
  • Thanks! A little playing around and I got it to work just right. I updated my question with your edits and how I got it to work. Thanks again!
    – NW Tech
    Dec 28 '13 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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