WordPress.org

Ready to get started?Download WordPress

Forums

Multiple loop problem... (5 posts)

  1. ThorHammer
    Member
    Posted 4 years ago #

    I have three loops om my index page in order to list last 5 posts according to GD Star Rating popularity in each of them. The listings are just title and a custom field related image. They are supposed to appear in 3 distinct columns, post 0-5,post 6-10, post 11-15.
    instead I get one huge one-column list looping in the endless and no break in the next column. And it is only the latest posts image used for all posts listed...Any skilled eye spotting anything strange in the first loop as presented below?:

    <div id="column_01">
    <?php query_posts('gdsr_sort=review&sort_order=asc&gdsr_multi=1&showposts=5&cat=3,5,6,9,31'); ?>
    
    <?php $posts = get_posts('numberposts=5&offset=0'); foreach ($posts as $post) : start_wp(); ?>
    <?php static $count1 = 0; if ($count1 == "5") { break; } else { ?>
    <?php
    if($post_image !== '') {
    ?>
    <h2><a href="<?php the_permalink() ?>"  rel="bookmark" title="Permanent url to <?php the_title(); ?>"><?php the_title(); ?><br />
    <img src="<?php echo $post_image; ?>" alt="<?php if($post_alt !== '') echo $post_alt; else the_title(); ?>" style="width:250px;height:190px " /></a></h2>
    <?php }
    else {
    echo 'missing image';
    } // endelse
    ?>
    
    <?php $count1++; } ?>
    <?php endforeach; ?>
    </div>
  2. alchymyth
    Forum Moderator
    Posted 4 years ago #

    1. this line:

    <?php $posts = get_posts('numberposts=5&offset=0'); foreach ($posts as $post) : start_wp(); ?>
    <?php static $count1 = 0; if ($count1 == "5") { break; } else { ?>

    in the foreach loop, you are setting the $count1 variable to 0 (zero) and immedeately after checking if it equals 5 - which can never be => endless loop.

    you could better move the initiation of the counter to before the 'foreach':

    <?php $posts = get_posts('numberposts=5&offset=0'); static $count1 = 0; foreach ($posts as $post) : start_wp(); ?>
    <?php  if ($count1 == "5") { break; } else { ?>

    2.
    where is $post_image defined?
    shouldn't be there some $post_image = get_post_meta(......); ??

  3. ThorHammer
    Member
    Posted 4 years ago #

    alchymyth: Thank you for your reply!! Aha, so it really IS an issue with the custom loop... I will try your solution!

    2: Sorry, I defined the variable ABOVE the custom query, the variable is equal to a post-related image, stored as a custom field value.

    I look forward to try your suggestion when I get back from work!!

  4. alchymyth
    Forum Moderator
    Posted 4 years ago #

    @ThorHammer:

    I defined the variable ABOVE the custom query, the variable is equal to a post-related image,

    imho, in this case you will show the same image five times (?)

    as far as i understood, you want to show five different post related images in your loop (?)

  5. ThorHammer
    Member
    Posted 4 years ago #

    alchymyth: eh.. *blush*... So that's why... I thought I could interfere with the query if I put the variable definition within the loop.
    Thank you. Again!

Topic Closed

This topic has been closed to new replies.

About this Topic