Support

Account

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 πŸ™‚