Created
January 3, 2022 16:29
-
-
Save atanemani/52f0fed86a8fa7937632ed1c8b18aede to your computer and use it in GitHub Desktop.
Get post terms in WordPress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* post terms */ | |
if (!function_exists('postTerms')) { | |
function postTerms($taxonomy, $single = true, $linked = false) | |
{ | |
global $post; | |
$terms = get_the_terms($post, $taxonomy); | |
if ($single) { | |
$term = reset($terms); | |
if ($linked) | |
return '<a href="' . get_term_link($term, $taxonomy) . '">' . $term->name . '</a>'; | |
return $term->name; | |
} else { | |
$output = ''; | |
foreach ($terms as $term) { | |
if ($linked) | |
$output .= '<a class="term-item" href="' . get_term_link($term, $taxonomy) . '">' . $term->name . '</a>'; | |
$output .= '<span class="term-item">' . $term->name . '</span>'; | |
} | |
$output .= ''; | |
return $output; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it works for me, thanks.