Support

Account

Home Forums General Issues Problems to output a field

Helping

Problems to output a field

  • Hi,

    i have a problem to output a field direct in the funktions.php.

    The code <?php the_field(‘anyfieldname’); ?>
    <?php echo get_field(‘anyfieldname’); ?>

    works in all php files – single.php, page.php etc but not in the functions.php.

    what i do wrong

    pls hlp

    thx

  • Hi @snoopdog1971

    The issue you had mostly occurred because your code was executed outside of The Loop. To fix it, you need to pass the post/page ID as the second parameter to the the_field() function like this:

    the_field('custom_field_name', 99);

    Where “99” is the post/page ID where the custom field is assigned. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/get_field/.

    To dynamically get the ID, you can use the global $post variable like this:

    global $post;
    the_field('custom_field_name', $post->ID);

    I hope this helps 🙂

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

The topic ‘Problems to output a field’ is closed to new replies.