Home Forums Front-end Issues Image wont display in header.php template file


Image wont display in header.php template file

  • Hey,
    I have an image set using ACF in the admin area for a custom post type, using the following in the header gets me an empty array:

    `$image = get_field(‘background_image’);

    The key is 100% correct it has been copied and pasted, I have also used the meta_key to no avail.

    I have tried every example on this page and nothing works…

    If I try:
    `$img = get_post_meta(81, ‘background_image’);

    I get:

    Array ( [0] => 82 )

    Any help would be great! I have been racking my brains all morning on this.


  • Since you are in header.php you are outside of “The Loop”, you will need to supply the get_field() function with the post ID;

    $queried_object = get_queried_object();
    $image = get_field('background_image', $queried_object->ID);
  • Thanks! This works… if I hardcode the post ID, so at the moment I have:

    $image = get_field('background_image', 81);

    if I do a dump on get_queried_object(); I get null.

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

The topic ‘Image wont display in header.php template file’ is closed to new replies.