custom field - How can I pass a variable set by ACF to header.php?

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

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());

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745624971a4636751.html

相关推荐

  • custom field - How can I pass a variable set by ACF to header.php?

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

    22天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信