1

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">
    </a>
<?php } ?>

And it doesn't work.

2

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

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

or

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.