bigdave
bigdave

Reputation: 337

inserting different div's after every 6 posts with paged posts

I've found lots on how to insert the same div every X number of posts but I'm struggling with how to insert a different div after X number of posts when the posts are paged, as the count resets back to 1 on each new page. So far I've managed to insert a div on page one and a different div on page 2 but I cant work out how to then insert a third div on page 3. I think I'm perhaps mis-using is_paged()?

<?php $counter = 1 ?>

<?php
    $args = array(
    'post_type'         => 'venues',
    'posts_per_page'    => 9,
    'orderby'           => 'name',
    'order'             => 'ASC',
    'paged'                 => get_query_var('paged'),
    );
    $new = new WP_Query($args);?>

    <?php if ( $new->have_posts() ) : while ($new->have_posts()) : $new->the_post();?>
        Loop content
        <?php if ($counter % 6 == 0 && !is_paged()) : ?>                       
            first additional div content                       
        <?php elseif ($counter % 6 == 0 && is_paged()) : ?>
            second additional div content
        <?php endif; ?>
    <?php $counter++; endwhile; endif;?>

It's probably worth mentioning that I'm using pagenavi

Upvotes: 1

Views: 67

Answers (1)

Peter Featherstone
Peter Featherstone

Reputation: 8102

is_paged() will be true on every page that suprisingly "is paged" so your if statement will always be false on any inner pages.

You probably need to use the get_query_var('paged') function to find out what page you are on - the below code is not tested but should work and if not it should at least point you in the right direction:

<?php $counter = 1;

    $args = array(
    'post_type'         => 'venues',
    'posts_per_page'    => 9,
    'orderby'           => 'name',
    'order'             => 'ASC',
    'paged'                 => get_query_var('paged'),
    );
    $new = new WP_Query($args);?>

    <?php if ( $new->have_posts() ) : while ($new->have_posts()) : $new->the_post();?>
        Loop content

        <?php if ($counter % 6 == 0 && !is_paged()) : ?>                       
            first additional div content                       
        <?php elseif ($counter % 6 == 0 && is_paged() && get_query_var('paged') == 2 ) : ?>
            second additional div content
        <?php elseif ($counter % 6 == 0 && is_paged() && get_query_var('paged') == 3 ) : ?>
            third additional div content
        <?php elseif ($counter % 6 == 0 && is_paged() && get_query_var('paged') == 4 ) : ?>
            fourth additional div content
        <?php endif; ?>
    <?php $counter++; endwhile; endif;?>

Upvotes: 2

Related Questions