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 made a customized index.php so that I have multiple loops on the same page so that the content can be displayed differently. I got this to work, however the pagination is broken. Despite several hours of Googling and trying all sorts of things, I can't get it to work. Subsequent pages just display the same thing as the home page.

I'm also using the WP-PageNavi plugin to do the pagination at the bottom. Each subsequent page is correct in the address bar and at the bottom of the page according to the plug-in, but the content is the same on every page.

I understand that you cannot have 'paged' and 'offset' at the same time, but I have absolutely no understanding of how to fix it.

Here is the code:

<!-- First Loop -->
    <div id="first-post">
        <?php
            $temp = $wp_query;
            $wp_query= null;
            $wp_query = new WP_Query();
            $wp_query->query('showposts=1'.'&paged='.$paged);

        ?>

        <?php $posts = get_posts('numberposts=1&offset=0'); foreach ($posts as $post) : start_wp(); ?>    

        <?php static $count1 = 0; if ($count1 == "1") { break; } else { ?>

                        <!-- post stuff -->

                     <?php $wp_query = null; $wp_query = $temp;?>

        <?php $count1++; } ?>
        <?php endforeach; ?>

    </div> <!-- close first-post -->

    <!-- Second Loop -->
    <ul class="two-col">
    <?php
            $temp = $wp_query;
            $wp_query= null;
            $wp_query = new WP_Query();
            $wp_query->query('showposts=4'.'&paged='.$paged);

    ?>

    <?php $posts = get_posts('numberposts=4&offset=1'); foreach ($posts as $post) : start_wp(); ?>
    <?php static $count2 = 0; if ($count2 == "4") { break; } else { ?>
             <li>
                        <!-- post stuff -->
    </li>
    <?php $count2++; } ?>
    <?php endforeach; ?>
    </ul> <!-- close two-col -->
    <?php $wp_query = null; $wp_query = $temp;?>

    <!-- Third Loop -->
    <ul class="three-col">

    <?php
            $temp = $wp_query;
            $wp_query= null;
            $wp_query = new WP_Query();
            $wp_query->query('showposts=6'.'&paged='.$paged);

        ?>
    <?php $posts = get_posts('numberposts=6&offset=5'); foreach ($posts as $post) : start_wp(); ?>
    <?php static $count3 = 0; if ($count3 == "6") { break; } else { ?>

    <li>

                        <!-- post stuff -->

    </li>

    <?php $count3++; } ?>
    <?php endforeach; ?>
    </ul> <!-- close three-col -->

    <?php $wp_query = null; $wp_query = $temp;?>

    <!-- End All Looping -->
share|improve this question
add comment

1 Answer

The content is the same because the loops are the result of the get_posts() call which does not take into account the page you are on. In short you are not looping through the results of your WP_Query but instead the get_posts() . Try replacing get_posts and the foreach with a while loop using the results of WP_Query .

Each of your loops should be of the form:

$wp_query = new WP_Query($args);

while ($wp_query->have_posts() ) : $wp_query->the_post();
//display contents here
endwhile;

For you purpose, $args would be 'showposts=4'.'&paged='.$paged where $paged is the page you are on.

If you are simply trying to output your posts with the first 1 styled one way, the next 5 styled another etc. It is probably best to have only one loop and but use some if statements inside to format the posts appropriately.

(NB if you are attempting to have several loops on one page, all of which paginate: without a bit of effort, that won't work. Since there is only one $paged variable for all the loops. )

share|improve this answer
    
Wow, thanks for the quick reply. Sorry if I sound ridiculous but I'm still new to php. How can I use an if statement to just change posts 2-4 and 5-12, but all be on one loop? That would be the ideal solution but I don't know how to do it. Thanks so much again. –  cw02 Jan 19 '12 at 2:30
add comment

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.