【问题标题】:Display advanced custom field (ACF) value from custom post type taxonomy - wordpress显示来自自定义帖子类型分类的高级自定义字段 (ACF) 值 - wordpress
【发布时间】: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


    【解决方案1】:

    术语名称和 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);
    

    【讨论】:

    • 我仍然得到一个 bool(false),当我检查我的 img src 中的元素时,我得到
    • 知道了,我必须添加这个:$url = $image['url'];
    猜你喜欢
    • 2018-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多