Home › Forums › General Issues › How to display the value from the latest post id in the widget › Reply To: How to display the value from the latest post id in the widget
Hi @ayeweb
That’s because the shortcode is trying to get the global $post
variable on your site, which doesn’t have the correct post ID if you put it in a widget.
In this case, you need to modify the template where you show the widget (usually sidebar) and show the data manually like this:
// get the latest post
$latest_posts = get_posts("post_type=post&numberposts=1");
// show the custom field
echo the_field('p1', $latest_posts[0]->ID);
This page should give you more idea about it: https://www.advancedcustomfields.com/resources/displaying-custom-field-values-in-your-theme/.
I hope this makes sense 🙂
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.