您需要使用get_the_terms()
要获取与帖子相关联的分类术语,并在其中循环,直到找到一个具有图像集的术语:
$post. = get_queried_object();
$terms. = get_the_terms( $post, \'taxonomy_name_here\' );
$image_url = null;
if ( $terms && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
$attachment_id = get_term_meta( $term->term_id, \'category-image\', true );
if ( $attachment_id ) {
$image_url = wp_get_attachment_image_url( $attachment_id, \'full\' );
break;
}
}
}
if ( $image_url ) {
// Output image as needed.
}