I have a custom taxonomy term of ‘artists’ which is the artist’s last name and a custom field of ‘first_name’. To create a list all all artists with both their first name and last name I am using the following code, but it is printing as two separate lists. Do I have to join tables (never done this) to accomplish this or is there another way to write it so they are all in one list?
<?php while ( have_posts() ) : the_post(); ?>
<?php
$args = array( 'taxonomy' => 'artists' );
$terms = get_terms('artists', $args);
$count = count($terms); $i=0;
if ($count > 0) {
$term_list = '<ul class="artist-list">';
foreach ($terms as $term) {
$i++;
$termid = 'artists_' . ($term->term_id);
$termfirst = the_field('first_name', $termid);
$term_list .= '<li><a href="' . get_term_link( $term->slug, $term->taxonomy ) . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $termfirst . $term->name . '</a></li>';
}
$term_list .= '</ul>';
echo $term_list;
}
?><?php endwhile; // end of the loop. ?>
The two lists are shown here on the development site
Replace
the_field
withget_field
. The former echoes data without the need to explicitly callecho
on the function name, the latter returns data, which is what you need when storing data against a variable for later use in your function. In your case, concatenating the first/last name prior to its output. That should fix your list.Although the tags have been edited (thanks toscho), in case anyone is wondering, since it’s not clear in the OP, the function
the_field
is not to be confused with the nativeget_post_meta
(and other) WordPress functions that handle retrieval of post meta data. Instead this particular function and it’s counterpart inget_field
are part of the plugin “Advanced Custom Fields”