WordPress.org

Support

Support » How-To and Troubleshooting » Splitting loop into groups of three

Splitting loop into groups of three

  • cykeltomte

    @cykeltomte

    Hi, I am trying to split a loop of post from a certain category into groups of three. I have managed to get the first group to show, but as soon as I try to add a new instance of the loop the page goes blank. Any ideas of what I am doing wrong?

    Above the second loop I have added this:

    <?php
    	$args = array(
    	'cat'			=> 3,
    	'posts_per_page'	=> 3,
    	'order'			=> 'ASC',
    	'offset'		=> 3
    	);
    	query_posts( $args );
    
    ?>

    Third loop would be offset 6 and so on.

    I want to have rows of three posts, where content can be expanded beneath a post. It looks good if I display all posts in a single loop, but when content from one post is expanded it screws up the layout beneath. That’s why I need to be able to group them as three, but I am happy to get other suggestions of how to make this work too 🙂

Viewing 7 replies - 1 through 7 (of 7 total)
  • vtxyzzy

    @vtxyzzy

    I am guessing that what you want is for the posts to be displayed in three columns and that you are using a ‘float: left’ style to get the posts to show in a row.

    If that is the case, the key is to insert a div after every third post that clears the float.

    You can use code like this:

    if (have_posts()) {
       while (have_posts()) {
          the_post();
          echo "<div class='post_content';>\n";
             echo "<p>";the_title();echo "</p>\n";
             the_excerpt();
          echo "</div>\n";
          if(++$post_count % 3 == 0 ) echo "<div class='clearboth'></div>\n";
       }
       echo "<div class='clearboth'></div>\n";
    }

    Then use style rules similar to this:

    .post_content {
       width: 30%;
       padding-left: 1%;
       float: left;
    }
    .clearboth { clear: both; }
    cykeltomte

    @cykeltomte

    Ah, thought somthing like that could solve it, just didn’t know how to do it. Thanks!

    On to another problem (should I make a new thread maybe?). As you can see here http://scp.se/scpwp/ the content is expanded beneath every post, however, i need the content to fill the full width of the page/body. Is it possible to put the content of every post in to an array, which gets reset efter every row, to post the content of three posts beneath every row? Or is it better to call a loop inside the loop in some way?

    cykeltomte

    @cykeltomte

    OK, solved it by placing the_content() of every post in an array which is cleared every third post.

    One question though, might it cause any problems to place the content in an array and posting it this way?

    vtxyzzy

    @vtxyzzy

    If you are using get_the_content(), or just $post->post_content, to put the content into the array, it may appear differently than when displayed with the_content().

    You will need to call apply_filters() before saving to the array to be sure all shortcodes are expanded and any other changes are applied:

    $contents[] = apply_filters(get_the_content());
    cykeltomte

    @cykeltomte

    Aha, so that’s why my shortcodes doesn’t work. Thank you, your’e a lifesaver!

    But, when I try apply_filters() my page goes blank. At the moment, I do this to put the contents in an array:

    $content = get_the_content();
    array_push($content_array,$content);

    I’ve tried:

    array_push($content_array,apply_filters(get_the_content());

    And:

    $content_array[] = apply_filters(get_the_content());

    Which both gave the same result, a blank page. Any idea of what I’m doing wrong?

    EDIT:

    $content = get_the_content();
    $content = apply_filters($content);
    array_push($content_array,$content);

    This doesn’t result in a blank page, but no content shows up.

    cykeltomte

    @cykeltomte

    Ok, may have solved it:

    $content_array[] = apply_filters('the_content',get_the_content());

    Would this work?

    EDIT:
    Seems like it works 🙂 Thanks for all the help! It has really helped me alot. I’m forever in your debt, but i hope a simple thanks will suffice.

    vtxyzzy

    @vtxyzzy

    You got it right. I mistakenly did not include the filter name in the code I posted.

    Glad you figured it out.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Splitting loop into groups of three’ is closed to new replies.