1

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:

http://pastebin.com/SVxYK0XP

Thanks

1
  • Please add relevant code samples to the question, rather than linking to a pastebin. Commented Nov 28, 2013 at 17:17

1 Answer 1

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');
Sign up to request clarification or add additional context in comments.

1 Comment

Glad I could help. Just make sure to always follow this pattern when using setup_postdata() to avoid future problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.