How to get tags and categories?

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?

Topic code functions tags categories Wordpress

Category Web


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.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.