Home › Forums › General Issues › Displaying post_object content within shortcode › Reply To: Displaying post_object content within shortcode
You are using this inside of a function (shortcode)
The main thing that you’re missing is a reference $post that you need to add at the top in order to use the standard post loop
global $post;
However, I would not do this because when using a shortcode you can never know how deeply nested the custom queries are and wp_reset_postdata()
will fail if your loop is nested inside an already existing secondary query loop.
Instead of using have_posts()
and I would not add the global reference above, I would do the following to avoid the potential problems
if (count($output->posts)) {
foreach ($output->posts as $post) {
....
$brand = get_field('brand', $post->ID);
}
}
So basically only use function that allow you to use the post ID rather than the normal posts loop.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.