admin管理员组

文章数量:1434914

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.

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.

Share Improve this question asked Apr 5, 2019 at 7:58 CatalinCatalin 235 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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());

本文标签: custom fieldHow can I pass a variable set by ACF to headerphp