I used the following code to get tags from the post and then simply echo them:
$posttags = get_the_tags($post->ID);
if ($posttags) {
foreach($posttags as $tag) {
echo $tag->name . ', ';}
}
Now I need to get the categories as well. My first idea was to simply duplicate the function but isn't there a way to create one single function that'll retrieve both the tags and categories and then have it echo?
I used the following code to get tags from the post and then simply echo them:
$posttags = get_the_tags($post->ID);
if ($posttags) {
foreach($posttags as $tag) {
echo $tag->name . ', ';}
}
Now I need to get the categories as well. My first idea was to simply duplicate the function but isn't there a way to create one single function that'll retrieve both the tags and categories and then have it echo?
get_the_terms( $id, $taxonomy );
is what you're looking for, I guess.
You can pass array as $taxonomy
param. So this snippet:
$posttags = get_the_terms($post->ID, array('category', 'post_tag'));
should do exactly what you're trying to achieve.
get_categories()
you could use maybe you can make a function and combine the two to return what you need. – Howdy_McGee ♦ Commented Nov 19, 2013 at 14:52get_the_terms
. It runs filters and uses cache, whilewp_get_post_terms
is more internal method. – Krzysiek Dróżdż Commented Nov 19, 2013 at 15:52