[resolved] Loop division and pagination (4 posts)

  1. SimonJ
    Posted 7 years ago #

    Hi all!

    Ok, here is my quest :

    I would like to divide my loop on my category/index/archives templates.

    I would simply put a banner between the first posts and the folowing posts.

    First post in the loop


    All the folowing posts

    And I want to keep this schema over the pagination, ie when I browse the older posts in category/archive/etc.

    Normally, I would do so with :

    [1] A query posts with showposts=1 = Display only one posts.

    [2] The banner I want to display

    [3] A query posts with offset=1 = Display all folowing posts.

    But this method wont works with the pagination to browse older posts.

    I know how to get the pagination working with a single query_posts, with this code : $page = (get_query_var('paged')) ? get_query_var('paged') : 1;

    But it doesn't seems to solve my actual problem with two loops...

    Am I clear enough? Anybody have an idea?



  2. richarduk
    Posted 7 years ago #

    How about creating a simple counter in your loop?

    I'm no php expert, but something along these lines:

    if $counter==1 {echo 'image.jpg'};

    I have no idea how that would work with pagination - does the variable get reset to 0 on each new page?

  3. SimonJ
    Posted 6 years ago #

    Sorry for the late feedback... I was out of town for holydays.

    I tried to play with your idea, the code you wrote above gaves me a good start for some google.

    Then, I found this article :


    The idea is to set the counter where you cass the "while have_posts" for the loop :

    <?php while (have_posts()) : the_post(); $counter++; ?>

    Then, call what you want anywhere before the "endwhile" with an "if" statement based on a counter value :

    <?php if ($counter == 2) : ?>
    <!--Do your stuff -->
    <?php endif ?>
    <?php endwhile; ?>

    Note the ($counter == 2) ... Just change the number to adjust your need. In this example, you will display your stuff right after the second post...

    And it works with the pagination

    VoilĂ ...


  4. WPContent.com
    Posted 6 years ago #

    Hi SimonJ

    I posted a video tutorial on how to do this. http://www.wpcontent.com/post/190. I hope that helps!


Topic Closed

This topic has been closed to new replies.

About this Topic