Skip to content

Instantly share code, notes, and snippets.

@atanemani
Created January 3, 2022 16:29
Show Gist options
  • Save atanemani/52f0fed86a8fa7937632ed1c8b18aede to your computer and use it in GitHub Desktop.
Save atanemani/52f0fed86a8fa7937632ed1c8b18aede to your computer and use it in GitHub Desktop.
Get post terms in WordPress
<?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;
}
}
}
@bvt7
Copy link

bvt7 commented Feb 8, 2023

it works for me, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment