WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Strange Loop Problem

[Resolved] Strange Loop Problem

  • http://www.gollygolly.com/category/photos/

    Go there, you will see my problem. I really don’t know what’s causing this. This is the only code in the page of any substance:

    <?php if (! empty($display_stats) ) { get_stats(); } else if (($posts & empty($display_stats)) ) : foreach ($posts as $post) : start_wp(); ?>
    <div class="entrycat">
    <?php $arc_query = new WP_Query('category_name=photos&orderby=post_date&order=DESC&showposts=-1'); ?> <?php while ($arc_query->have_posts()) : $arc_query->the_post(); ?><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php echo $post->post_excerpt; ?></a>
    <?php endwhile; ?>
    </div>
    <?php endforeach; else: ?>

    Any ideas on what I need to change?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Kafkaesqui
    Member

    @kafkaesqui

    You have one posts loop (generated through WP_Query()) nested in another (foreach ($posts as $post)), and this is causing the displaying of each post multiple times. 5 times 5, to be exact.

    At this point I wouldn’t know what to suggest to replace the code above as I’ve no idea what is important and what isn’t. But you can try whittling it down to:

    <div class="entrycat">
    <?php $arc_query = new WP_Query('category_name=photos&orderby=post_date&order=DESC&showposts=-1'); ?> <?php while ($arc_query->have_posts()) : $arc_query->the_post(); ?><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php echo $post->post_excerpt; ?></a>
    <?php endwhile; ?>
    </div>

    And go from there.

    How would I close that? This is what I have tried putting after the code you suggested:

    <?php endforeach; else: ?>
    <div class="center">
    <h2>Not Found</h2>
    </div>
    <?php endif; ?>

    </div>
    <?php get_sidebar(); ?>

    </div>
    <?php get_footer(); ?>

    I get page errors for the ‘foreach’ statement, but I can’t seem to find anything that works.

    Kafkaesqui
    Member

    @kafkaesqui

    What I gave above is a complete statement or code block. You don’t close it with endforeach or endif because it’s a while statement, hence the endwhile at the end. But if you need a conditional, you can change it to:

    <?php if($arc_query->have_posts()) : while($arc_query->have_posts()) : $arc_query->the_post(); ?>
    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php echo $post->post_excerpt; ?></a>
    <?php endwhile; else : ?>
    <div class="center">
    <h2>Not Found</h2>
    </div>
    <?php endif; ?>

    I don’t really NEED the conditional, so I just ended it with the while. It worked perfectly. Thank you so much, I appreciate the help. I am pretty good with code but querys are completely strange to me. Thank you.

    If you, or anyone could take a look – http://www.gollygolly.com/category/photos/ – That is the page where I am using this code. In IE the photos (excerpts) don’t show up! It’s funny because the source code says that they are there, but nothing is there to see. Any ideas?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] Strange Loop Problem’ is closed to new replies.