Glen Wheeler
Glen Wheeler

Reputation: 3

Stop looping in Wordpress

I have had a look through Stack overflow and cant see something similar to this. I have tried a few things and still cannot get it to work.

I am trying to display something else after this piece of code, but it does not appear. When I move it above this piece of code it works fine so I think that the code below is still lopping. Can some one please tell me how I can stop the loop and then continue to display what I need to display.

Thanks.

the code is as follows that I need to stop looping

<?php $pst = get_posts(array('numberposts' => 2, 'post_type' => 'testimonials', 'orderby' => 'menu_order', 'order' => 'ASC' )); foreach($pst as $post): setup_postdata($post); ?>
  <li>
    <?php the_post_thumbnail('testimonial-thumb'); ?>
    <?php the_content(); ?>
    <h5><?php the_field('testimonial_name'); ?>
    <span><?php the_field('testimonial_job_position'); ?>, <?php the_field('testimonial_company'); ?></span>
    </h5>
  </li>
<?php endforeach; ?>

Upvotes: 0

Views: 124

Answers (1)

Ennui
Ennui

Reputation: 10190

You didn't provide enough information to accurately diagnose your problem but based on the fact that it works above the foreach but not after, it seems likely that your problem is related to the global $post object in Wordpress.

Try adding wp_reset_postdata(); AFTER the endforeach; line.

This will restore the $post object to the one for that page, rather than for the last of the post objects returned by get_posts(). Essentially it just cleans up after the setup_postdata() call. Without doing this, template tags and functions that rely on the $post object will not function as expected.

Upvotes: 1

Related Questions