WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Strange Loop Problem (6 posts)

  1. faustina
    Member
    Posted 7 years ago #

    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?

  2. Kafkaesqui

    Posted 7 years ago #

    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.

  3. faustina
    Member
    Posted 7 years ago #

    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.

  4. Kafkaesqui

    Posted 7 years ago #

    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; ?>

  5. faustina
    Member
    Posted 7 years ago #

    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.

  6. faustina
    Member
    Posted 7 years ago #

    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?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags