【发布时间】:2017-06-02 15:56:38
【问题描述】:
在 Wordpress 中,我创建了一个名为“Sports”的自定义帖子类型,其中包含一个名为“sport_locations”的分类。使用高级自定义字段 (ACF) 插件,我创建了在分类术语中显示的字段。我得到了一切工作,但是我无法输出我上传的图像。
在 ACF 中,我可以选择将图像的返回值设为:对象、url 或 ID。现在我把它设置为对象。
下面是我到目前为止的代码。我在 single-sports.php 中编写这段代码。我创建了一个 foreach 循环,只吐出与该特定运动相关的术语。
当我执行 var_dump 时,我不断得到 bool(false)。很抱歉在代码中添加了额外的注释,我尝试了很多不同的东西,并认为我会把它留在我的代码中作为参考
帖子类型 = 运动
分类法 = 运动地点
acf 字段 = location_image(返回值 = 对象)
<?php
$terms = get_the_terms( $post->ID , 'sport_locations' );
//$LocImg = $wp_query->queried_object;
// echo '<pre>';
// echo var_dump($LocImg);
// echo '</pre>';
foreach ( $terms as $term ) {
$term_thumb = get_field('location_image', 'sport_locations'.$term->term_id);
echo '<pre>';
echo var_dump($term_thumb);
echo '</pre>';
echo '<div class="sport-single">';
echo'<img src="' .$term_thumb['url']. '" class="img-responsive">';
//echo '<img src="' .$locationImage[0]. '" class="placeholder" />';
//echo '<img src="' .get_src_picture(get_field("location_image", "sport_locations".$LocImg->term_id), "category"). '" class="img-responsive" />';
//echo '<img src="' .get_field( "location_image", $LocImg->taxonomy . "_" . $LocImg->term_id ). '" />';
echo '<p><a href="/sport_location/'.$term->slug .'/">'.$term->name .'</a></p>';
echo '</div>';
}
?>
【问题讨论】:
标签: php wordpress foreach taxonomy