Ste*_*e R 6 php wordpress custom-fields custom-taxonomy advanced-custom-fields
我已经为我的布道自定义帖子类型创建了一个自定义的单个sermons.php模板文件,并希望在此帖子中包含布道说话者的讲道图像自定义字段.
自定义分类ID:sermon_speaker自定义字段ID:sermon_speaker_image
我已经能够获得分类学术语ID以在页面上显示为数字,使用:
<?php
$terms = wp_get_post_terms($post->ID, "sermon_speaker");
foreach ($terms as $termid) {
echo $termid->term_id;
}
?>
Run Code Online (Sandbox Code Playgroud)
我正在尝试弄清楚如何在我目前拥有的代码中使用此术语ID.$ term_id,以便将术语ID添加到背景图像URL的末尾.
<div class="sermon-speaker-image" style="background-image: url('<?php the_field( 'sermon_speaker_image', 'sermon_speaker_' . $term_id ); ?>');"></div>
Run Code Online (Sandbox Code Playgroud)
更新:以下代码是基于以下答案的工作解决方案:
<?php
global $post;
// load all 'sermon_speaker' terms for the post
$terms = get_the_terms($post->ID, 'sermon_speaker');
// we will use the first term to load ACF data from
if( !empty($terms) )
{
$term = array_pop($terms);
$custom_field = get_field('sermon_speaker_image', $term );
}
?>
<div class="sermon-speaker-image" style="background-image: url('<?php echo $custom_field; ?>');"></div>
Run Code Online (Sandbox Code Playgroud)
尝试这个代码,这应该适合你
global $post;
// load all 'sermon_speaker' terms for the post
$terms = get_the_terms($post->ID, 'sermon_speaker');
// we will use the first term to load ACF data from
if( !empty($terms) )
{
$term = array_pop($terms);
$custom_field = get_field('sermon_speaker_image', $term );
// do something with $custom_field
//i.e. echo $custom_field;
//i.e. echo "<img src='$custom_field'/>";
?>
<div class="sermon-speaker-image" style="background-image: url('<?php echo $custom_field; ?>');"></div>
<?
}
Run Code Online (Sandbox Code Playgroud)
您可以阅读高级自定义字段的官方文档了解更多信息
| 归档时间: |
|
| 查看次数: |
1197 次 |
| 最近记录: |