Last active
September 10, 2019 12:19
-
-
Save Inzman/8abd6ee4656f0fc4068448c194f08190 to your computer and use it in GitHub Desktop.
Get Wordpress post terms
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
// wp_get_object_terms() always queries the database | |
/* If you’re looping over WP_Query results, you should prefer get_the_terms() instead. | |
It’s pretty much the same for most use cases, but it uses the object cache, | |
which by default gets populated with the terms for the posts matching your query — unless you specifically | |
set update_post_term_cache as false when instantiating WP_Query. | |
*/ | |
// Get terms for post | |
if ( ! function_exists( 'storefront_post_terms' ) ) { | |
function storefront_post_terms(){ | |
$terms = get_the_terms($post->ID, 'product_cat'); | |
if($terms != null) { | |
end($terms); | |
$endKey = key($terms); | |
foreach($terms as $key => $term) { | |
echo $key != $endKey? $term->name.", " : $term->name; | |
unset($term); | |
} | |
} | |
} | |
} | |
// Get terms for post (with array) | |
$terms = get_the_terms($post->ID, 'skills'); | |
if($terms != null) { | |
$output = array(); | |
foreach($terms as $term) { | |
$output[] = $term->name; | |
unset($term); | |
} | |
echo implode(", ", $output) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment