WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. kortschot
    Member
    Posted 2 years ago #

    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

  2. kortschot
    Member
    Posted 2 years ago #

  3. Chip Bennett
    Theme Review Admin
    Posted 2 years ago #

    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();
  4. kortschot
    Member
    Posted 2 years ago #

    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.

  5. alchymyth
    Forum Moderator
    Posted 2 years ago #

    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)

  6. kortschot
    Member
    Posted 2 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic