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 badges1 Answer
Reset to default 2You 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
版权声明:本文标题:custom field - How can I pass a variable set by ACF to header.php? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745624971a2666916.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论