WordPress: Can’t see custom field after the Loop

I have created a loop to show some products on my wordpress site and they seem to be working fine they reel out the products however if I place one of custom fields after the loop it doesn’t show. I know its not an issue with the custom field itself as it works fine if I put it above the loop. Does anyone know where I could be going wrong?

Here is my code:

Read More

http://pastebin.com/SVxYK0XP

Thanks

Related posts

Leave a Reply

1 comment

  1. You are calling setup_postdata() within your loops, therefore overwriting the $post object.
    When you are calling the_field('monoblock_valves_text'); after the foreach loop, it’s trying to get that custom field out of the last post of the loop, while it clearly needs to get it from the actual post/page showing.

    You need to store the old $post object before the loop, and restore it after the loop, as such:

    $old_post = $post;
    foreach($products_mono_posts as $post):
      setup_postdata($post);
      // Rest of code
    endforeach;
    $post = $old_post;
    setup_postdata($post);
    
    the_field('blahblahblah');