Support » Plugins » Multiple Loops and Pagination

  • Resolved matharvard


    Hi there, I’ve done quite a bit of searching and I still couldn’t find a solution to my problem. Basically, I have two instances of the Loop: the first is limited to 1 post. The second has no post limit but has an offset of 1 (since the first post was displayed in the first loop).

    That being said, whenever I try to view previous pages (wordpress/page/2/), it doesn’t change the content at all, it’s as if I was viewing the index page. I don’t know whether this is a bug, or is just not possible. Any help would be greatly appreciated, and thank you in advance.

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thanks, I’ve tried that fix before though, but it still doesn’t function properly. Here’s a simplified version of my code:

    I’m pretty sure I’ve implemented it correctly, any suggestions?

    First, what are you trying to accomplish by having 2 loops? Your code simply displays the latest post in the first loop, and offsets to show the next two in the second. Your “simplified version” doesn’t really show me *why* you would need two loops to accomplish what one (and the default) already does.

    Second, pagination is going to be pretty confused duplicating it within both post loops. For example, the first post loop will assume you are paging through pages displaying 1 post, while the latter one assumes 2. That’s unavoidable. Even working this out, the ‘offset’ would need to take pagination into account.

    So, what are you trying to accomplish by having two post loops, and what do you expect each to display on paginated queries?

    The two post loops have a design element between them. I display the first post, so it stands out and has focus, then insert (in this case) a featured content bar, and finally display the other posts. The number of posts it displays in the second loop doesn’t matter, just as long as it is offset by 1 (I added the 2 post limit to test).

    For pagination, I just need it to act like a normal loop, so whichever posts aren’t displayed on the first page, will be on the second, or third, etc. While maintaining the break after the first post for the featured content bar.

    ## Featured Content ##

    That’s what it’s meant to look like, and thanks a lot for helping out 🙂

    Just found a fix: I set up a counter that increments after each post, and if it’s set to 1 (on the first post) it prints out the extra HTML I need. So it keeps everything in one loop. Thanks a lot for your help you, I really appreciate it.

    If the posts are expected to ‘paginate’ as normal and you are merely inserting this featured content in at the end of the first post on any page, using the post counter solution in a single loop is the best option. Another is similar to this, in that you can test for the first post.

    Within the loop where this featured content bit goes you would add:

    <?php if( $post == $posts[0] ) : ?>
    <?php endif; ?>

    This tests the current post ($post) in the loop against the first in the posts object. When they’re the same (which they will be for the first post on every page), your featured content section displays.

    Hi. Im having the same problem, but when reviewing this post i didnt quite obtain the solution since matharvard just solved it but didnt post what his answer was…

    ive got 2 loops, which display in two different horizontal rows with 12 posts each… first loop displays posts 1-12, second one offsets by 12… when i click on “previous posts” posts dont change…

    "if" begins with
    <?php if (have_posts()) : ?>
    <?php $post = $posts[0]; // Thanks Kubrick for this code ?>
    posts with
    <?php $my_query = new WP_Query('showposts=12');
    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID; ?>
    and for the navigation:
    <?php posts_nav_link( ' · ',  __('&laquo; Discos Posteriores'), __('Discos anteriores &raquo;'), __('') );?>

    also… im using a java plugin qwicks for display thus posts are contained within a “ul” and displayed as “li”… when on “/page/2/” formatting is not mantained…

    would certainly appreciate any help…

    try the below code ↓

    <?php $cnt = 0; ?>
    <?php while (have_posts() && $cnt == 0) : the_post(); ?>
    ... post loop stuff here ...
    <?php $cnt++; endwhile; ?>
    ... your between post content here ...
    <?php rewind_posts();
    $cnt = 0;
    while (have_posts()) : the_post(); if ($cnt >= 1 ): ?>
    ... post loop stuff here ...
    <?php endif; $cnt++; endwhile; ?>

    Thanks for the reply Chaoskaizer, the code works except for a couple of things…

    This resolves Matharvard’s issue, but doesnt quite resolve mine…

    Where he wanted the first post in the first loop and then the rest of the posts in the second loop, i wanted the first 12 posts, and then the next 12…

    and even with the code when /page/2/ is loaded my “ul” which displays inline “li” (works fine in index) does not maintain its format… i.e. does not display inline but rather a vertical list.

    fixed it using different code…

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Multiple Loops and Pagination’ is closed to new replies.