Home › Forums › Front-end Issues › How to display taxonomy labels
Hi all,
I’m developing a custom portfolio item with a lot of different (repeatable) fields. I created a row where the user can select one or more from the available portfolio categories to indicate what kind of portfolio item it is (a row with the type ‘Taxonomy’).
But all I get on the front-end are the category-id’s.
In my ‘single.php’ I have:
function whd_portfolio_page() {
echo '<article class="entry dynamic">';
echo '<div class="entry-content">';
// check if the flexible content field has rows of data
if( have_rows('portfolio_page') ):
// loop through the rows of data
while ( have_rows('portfolio_page') ) : the_row();
if( get_row_layout() == 'intro' ):
echo '<p>';
the_sub_field('text');
endif;
if( get_row_layout() == 'project_summary' ):
$field = get_field_object('project_summary');
$value = get_field('project_summary');
$label = $field['choices'][ $value ];
echo '<div id="summary">';
echo '<h3 class="summary-title">Project summary</h3>';
echo '<p class="details">';
echo '<span class="acf-field">Client:</span><span class="acf-value">' . get_sub_field('client') . '</span><br>';
echo '<span class="acf-field">Year:</span><span class="acf-value">' . get_sub_field('year') . '</span><br>';
echo '<span class="acf-field">Expertise:</span><span class="acf-value">';
echo implode(' | ', get_sub_field('expertise_new'));
echo '</span><br>';
echo '</p></div>';
endif;
if( get_row_layout() == 'visual' ):
echo '<div class="project-image">';
echo '<img src="';
the_sub_field('visual');
echo '"/>';
echo '<div class="project-image-caption">';
the_sub_field('image_caption');
echo '</div>';
echo '</div>';
endif;
if ( get_row_layout() == 'in-depth'):
the_sub_field('in_depth');
endif;
endwhile;
endif;
echo '</article>';
echo '</div>';
}
but this only shows (on the front-end):
Expertise: 23 | 17
But it should be:
Expertise: Photoshop | Illustrator
(Look at the echo implode(' | ', get_sub_field('expertise_new'));
section)
Doing the same with a row with the type ‘Checkbox’ it does what I want because I filled in the different options in the appropriate ‘Choices’ field.
How can I display the name of the categories (Taxonomy names)?
Any help will be appreciated.
A taxonomy field can either return term object or term id. From what is being displayed I’m assuming that you are returning term id and you should change this to term object. Then you’ll need to loop through the array that’s returned and echo the term names.
$terms = get_field('expertise_new');
if ($terms) {
echo '<span class="acf-field">Expertise:</span>
echo <span class="acf-value">';
$term_names = array();
foreach ($terms as $term) {
$term_names = $term->name;
}
echo implode(' | ', $term_names),'</span><br>';
}
John,
Thanks for your reply.
I got it to work thanks to your little code-snippet.
The only thing I changed was the populating of the array in the foreach loop:
$term_names[] = $term->name;
By adding these square brackets you ‘tell’ the array to add the new value instead of overwriting it, which is the case if you don’t add those square brackets.
Yep, I forgot the [], sometimes I do that and then wonder why I’m not getting an array. Clad you caught it.
The topic ‘How to display taxonomy labels’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.