WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Nested Loop: Loop 6 posts, Loop same w/ different code, Repeat (6 posts)

  1. S3nd41
    Member
    Posted 2 years ago #

    Hi guys,

    I'm trying to get a nested loop working that will display 6 posts with a certain code, then go back, display the same 6 posts with a different code and then continue doing this until there are no more posts.

    Can anyone give me some pointers on how this can be accomplished? I tried searching the forums but the solutions are really specific and I'm not sure how to adapt them.

    Thanks!

  2. alchymyth
    Forum Moderator
    Posted 2 years ago #

    http://codex.wordpress.org/The_Loop#Multiple_Loops

    you might need to use rewind_posts() http://codex.wordpress.org/Function_Reference/rewind_posts after each loop; and possibly use the 'offset' parameter for the query http://codex.wordpress.org/Class_Reference/WP_Query
    and all of that in a foreach or while loop ...

    just checking: you mean:

    A B C D E F a b c d e f
    G H I J K L g h i j k l
    M N ...

    are you working with a particular theme?

  3. S3nd41
    Member
    Posted 2 years ago #

    No, I'm building my custom theme.
    I've found some code that I've adapted and so far I've managed to get the loop to repeat twice on the first 6 posts, but after those first 6 it simply loops the remaining posts one time.

    Here's the code:

    http://pastebin.com/hBkYvy6U

    The problem is that the second loop is only looping once after the first one prints the first 6 posts, how could I adapt this so that the second loop executes everytime?

    [No bumping. If it's that urgent, consider hiring someone.]

  4. alchymyth
    Forum Moderator
    Posted 2 years ago #

    you need to reset the $counter or check for a multiple of 6 with some code like if( $counter%6 == 0 )

  5. S3nd41
    Member
    Posted 2 years ago #

    It's not urgent but somewhat like 90% of the threads I create/participate in remain unanswered, (either general threads or plug-in support requests). No point in creating a forum if there's no actual participation from the community, I think. That put, thanks for the tip!

  6. S3nd41
    Member
    Posted 2 years ago #

    Managed to get a working piece of code with help from:
    http://stackoverflow.com/questions/8901481/wordpress-nested-loop-loop-6-posts-loop-same-w-different-code-repeat

    Here's the final result:
    http://pastebin.com/guccGZE9
    if someone else wants to do something like this.

Topic Closed

This topic has been closed to new replies.

About this Topic