显示来自自定义帖子类型分类的高级自定义字段(ACF)值 - wordpress

问题描述:

在Wordpress中,我使用名为“sport_locations”的分类标准创建了一个名为“体育”的自定义帖子类型。使用高级自定义字段(ACF)插件,我创建了在分类术语上显示的字段。我有一切工作,但我无法输出我上传的图像。显示来自自定义帖子类型分类的高级自定义字段(ACF)值 - wordpress

在ACF中,我可以选择图像的返回值:对象,网址或ID。现在我已经设置了对象。

以下是我到目前为止的代码。我在single-sports.php中写这段代码。我创建了一个foreach循环,只是吐出与特定运动相关的术语。

当我做了var_dump我不断收到bool(false)。很抱歉的额外代码注释掉我尝试了一堆不同的事情,想我会留在我的代码作为参考

类型=体育

分类= sport_location

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>'; 

    } 
?> 

应该在术语名称和ID之间有一个下划线。所以......

$term_thumb = get_field('location_image', 'sport_locations'.$term->term_id); 

...应该是...

$term_thumb = get_field('location_image', 'sport_locations_'.$term->term_id); 

或者,您可以通过术语对象...

$term_thumb = get_field('location_image', $term); 
+0

我仍然得到一个布尔(false),当我检查我的img src里面的元素时,我得到了 Mikey

+0

得到它我不得不添加这个:$ url = $ image ['url']; – Mikey