WordPress.org

Support

Support » Themes and Templates » Loop is looping stuff I don't want to be Looped =)

Loop is looping stuff I don't want to be Looped =)

  • He there…

    I got a WP loop that has to put out a list of news items.

    The 1th column is the latest post in the category news, the second column shows the second, third and fourth post in the same category “news” and the third and last column is listing the fifth+ titles from also the same category.

    The problem is that I can’t seem to figure out why and how the loop is putting the ie. col_three in the loop. I’m guessing I need to stop the loop somehow, open the div class col_three, and start the loop again.

    Hope I make some sense…

    This is what I got:

    [Code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

    Above and below this section I query posts from a different category.

    <section>
    	<article id="">
    		<?php query_posts('cat=1&posts_per_page=5');
    			if (have_posts()) : while (have_posts()) : the_post();
    			 	 the_title ();
    			 	 the_content();
    			 endwhile; endif; ?>
    	</article>
    </section>

    Thanks in advance!
    Paul

Viewing 5 replies - 1 through 5 (of 5 total)
  • Chip Bennett

    @chipbennett

    Theme Review Admin

    Your problem is that you’re using query_posts().

    Do not use query_posts() for secondary loops!

    Use WP_Query() or get_posts() instead.

    For example:

    $cat_query_args = array(
        'cat' => 1,
        'post_per_page' => 5
    );
    $cat_query = new WP_Query( $cat_query_args );

    Then, you just have to set up the loop slightly differently:

    if ( $cat_query->have_posts() ) : while ( $cat_query->have_posts() ) : $cat_query->the_post();
        the_title();
        the_content();
    endwhile; endif;
    
    // THIS IS VERY IMPORTANT
    wp_reset_postdata();

    thanks, ill start playing with this.

    I’m not that good with PHP, so its a trial and error thing for me. But one must learn.

    Anyways thanks for the input, and I’ll post new code when I stumble upon a new flaw or solution.

    Have a nice weekend.

    Moderator Michael

    @alchymyth

    Forum Moderator

    to pack post 2 to 4 into .col_two and all others into .col_three, try:

    http://pastebin.com/cYpfpqs9

    the code has some added conditional statements for the output of the opening and closing divs;
    for instance:

    <?php if ($count == 4 || ($list_news->current_post+1) == $list_news->post_count ) : ?>

    this line checks if it is after the fourth post or if all posts are gone (i.e. if there were less than four posts in the loop)

    awesome, this works like a glove.

    Just pasted in in my project and works instant. I’m going to dissect your snippet tomorrow so I understand it.

    Thanks, you just made my weekend!

    With Regards,
    Paul

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Loop is looping stuff I don't want to be Looped =)’ is closed to new replies.