I have created a custom field with Advanced Custom Fields for my posts. My variable is set product_image, so I can print it with:

<?php the_field('product_image'); ?>

But I need to print it in my site-header section, within header.php, only for single posts.

<?php if( is_single('post') ) { ?>
    <img src="<?php the_field('product_image'); ?>">
<?php } else { ?>
    <a href="<?php echo esc_url( home_url( '/' ) ); ?>">
        <img src="<?php echo esc_url( home_url( '/' ) ); ?>/logo.png">
<?php } ?>

And it doesn't work.


You can pass the post id as the 2nd argument on the_field.

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


the_field('product_image', get_the_ID());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.