This is how you can print taxonomy terms form a custom vocabulary in the post template. WordPress print taxonomy terms from custom vocabulary.
WordPress list taxonomy terms from custom vocabulary
In the following example my custom vocabularies are: pods_tax_industry
, pods_tax_profile
and pods_tax_service.
I get the terms using get_terms()
. I add them to an array named $special_terms[].
I loop through the special taxonomy vocabularies and inside the loop I loop through the individual terms. I get the links to the terms using get_term_link()
function.
$has_items = false;
$post_term_links = "";
$tindustry = get_terms(
array(
'taxonomy' => 'pods_tax_industry',
'hide_empty' => true,
)
);
$tprofile = get_terms(
array(
'taxonomy' => 'pods_tax_profile',
'hide_empty' => true,
)
);
$tservice = get_terms(
array(
'taxonomy' => 'pods_tax_service',
'hide_empty' => true,
)
);
$special_terms[] = $tindustry;
$special_terms[] = $tprofile;
$special_terms[] = $tservice;
foreach ($special_terms as $terms) {
$html_output = '<div class="post_terms">';
foreach ( $terms as $tag ) {
$has_items = true;
$tag_link = get_term_link( $tag->term_id );
$html_output .= "<a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
$html_output .= "{$tag->name}</a> ";
}
$html_output .= '</div>';
if ($has_items) {
$post_term_links .= $html_output;
}
}
?>
Reference to get_terms() on WordPress.org
Taxonomy terms that the current post is tagged with
I get the terms using wp_get_post_term()
. I add them to an array named $special_terms[]
and proceed the same way as previously.
<?php
$post_id = $post->ID;
$post_term_links = "";
$tindustry = wp_get_post_terms( $post_id, 'pods_tax_industry');
$tprofile = wp_get_post_terms( $post_id, 'pods_tax_profile');
$tservice = wp_get_post_terms( $post_id, 'pods_tax_service');
$special_terms[] = $tindustry;
$special_terms[] = $tprofile;
$special_terms[] = $tservice;
foreach ($special_terms as $terms) {
$html_output = '<div class="post_terms">';
foreach ( $terms as $tag ) {
$tag_link = get_term_link( $tag->term_id );
$html_output .= "<a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
$html_output .= "{$tag->name}</a> ";
}
$html_output .= '</div>';
$post_term_links .= $html_output;
}
?>
Printing the terms
Later in the template file I just check if the variable is not empty and print it.
<?php if(!empty($post_term_links)): ?>
<div class="post-tag-link">
<?php echo $post_term_links ?>
</div>
<?php endif; ?>
Alternatively to get a quick list of terms you can do the following
// To get a quick List of the terms you can always use wp_list_pluck then use Join with a "," as separator.
$term_obj_list = get_the_terms( $post_id, 'pods_tax_industry' );
if(!empty($term_obj_list)){
$the_terms= join(', ', wp_list_pluck($term_obj_list, 'name'));
echo $the_terms;
}
Reference to wp_get_post_terms on WordPress.org
I hope you found this useful! Please like the page if you did, cheers!