This seems very straight forward, but I can't get it to work even after going through the documentation (and a number of questions in this site) multiple times.
I have a custom post type called comic
with a taxonomy field (type radio button) called book
. The book
taxonomy has a text field called book_title
. I'm trying to display the value of book_title
in single-comic.php
.
This is what I've done so far:
<?php
$term = get_field('book');
if( $term ): ?>
<h1><?php echo $term->book_title; ?></h1>
<?php endif; ?>
This results in an empty h1
element, which suggests that $term
returns true
. Also, echoing $term
returns 9
which I believe is the number of fields (default and custom) associated with book
taxonomy. This means that I'm getting the correct object. I'm just unable to use it to display the values of its fields.
You will get array in a multi dimensional format so you cannot fetch directly like you have implemented. To fetch respective field you need to loop the $terms array. Please try below snippet. Hope it works.
<?php
$terms = get_the_terms(get_the_ID(), 'book');
if( $terms ): ?>
<?php foreach( $terms as $term ): ?>
<h1><?php echo $term->book_title; ?></h1>
<?php endforeach; ?>
<?php endif; ?>