Support

Account

Home Forums General Issues How to display the value from the latest post id in the widget

Solved

How to display the value from the latest post id in the widget

  • Normally have to add p=.. the id of the post
    I would like to remove p=.. and the id of post will be the latest post id.
    Just display in the widget.
    Thank you.

  • Hi @ayeweb

    Thanks for the post.

    I am not sure I have clearly understood this use case, but I would like to point that you can target anh ACF value from the $_POST data like so:

    $value = $_POST['acf']['field_key'];

  • @James @acf-support
    My shortcode in widget is:
    [acf field=p1 post_id=1277]

    And i have to change 1277 to the new post id every day.
    I would like to remove them and it will be like:
    [acf field=p1]
    For get the latest post id, then i will not change the new post id every day.
    But it not show anything.

    Thank you.

  • 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 🙂

  • OH, This is the nice one it’s work!.
    Thanks

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘How to display the value from the latest post id in the widget’ is closed to new replies.