首页 文章

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

提问于
浏览
0

在Wordpress中,我使用名为“sport_locations”的分类法创建了一个名为“Sports”的自定义帖子类型 . 使用高级自定义字段(ACF)插件,我创建了字段以显示分类术语 . 我把一切都搞定了,但是我输出上传的图像时遇到了麻烦 .

在ACF中,我可以选择图像的返回值:对象,URL或ID . 现在我把它设置为对象 .

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

当我做var_dump时,我不断得到bool(假) . 很抱歉代码中的额外评论我正在尝试一堆不同的东西,并想通知我会把它留在我的代码中作为参考

帖子类型=体育

分类法= sport_location

acf field = 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>';

    }
?>

1 回答

  • 0

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

相关问题