如何获得分类的自定义字段值?

问题描述

我在分类法中添加一个自定义字段,称为头像演员

enter image description here

enter image description here

如何在我的帖子中显示它?

<?PHP 
    $queried_object = get_queried_object(); 
    $taxonomy = $queried_object->taxonomy;
    $term_id = $queried_object->term_id;
    
    $actor_avatar = get_field('actor_avatar',$taxonomy . '_' . $term_id);
    $actor_eng_name = get_field('actor_english_name',$taxonomy . '_' . $term_id);
    $actor_dob = get_field('actor_dob',$taxonomy . '_' . $term_id);
    $actor_nation = get_field('actor_nation',$taxonomy . '_' . $term_id);
    $actor_job = get_field('actor_job',$taxonomy . '_' . $term_id);
    $actor_blood = get_field('actor_blood',$taxonomy . '_' . $term_id);
    $actor_forte = get_field('actor_forte',$taxonomy . '_' . $term_id);
    $actor_interests = get_field('actor_interests',$taxonomy . '_' . $term_id);
    $actor_height = get_field('actor_height',$taxonomy . '_' . $term_id);
    $actor_weight = get_field('actor_weight',$taxonomy . '_' . $term_id);
    $actor_gender = get_field('actor_gender',$taxonomy . '_' . $term_id);
    $actor_info = get_field('actor_info',$taxonomy . '_' . $term_id);
?>

解决方法

您可以使用 get_the_termsget_field。检查下面的代码。

<?php 

global $post;
$terms = get_the_terms( $post->ID,'category' );
foreach ( $terms as $term ) {
    $term_object = $term;
    break;
}

$actor_avatar    = get_field('actor_avatar',$term_object);
$actor_eng_name  = get_field('actor_english_name',$term_object);
$actor_dob       = get_field('actor_dob',$term_object);
$actor_nation    = get_field('actor_nation',$term_object);
$actor_job       = get_field('actor_job',$term_object);
$actor_blood     = get_field('actor_blood',$term_object);
$actor_forte     = get_field('actor_forte',$term_object);
$actor_interests = get_field('actor_interests',$term_object);
$actor_height    = get_field('actor_height',$term_object);
$actor_weight    = get_field('actor_weight',$term_object);
$actor_gender    = get_field('actor_gender',$term_object);
$actor_info      = get_field('actor_info',$term_object);

?>