How to get post categories and tags in wordpress?
For retrieving the post categories & post tags , we can use get_the_category and get_the_tags functions in wordpress. Here is the code snippet for both functions.
get_the_category() example:
<?php
$categories = get_the_category();
if ( ! empty( $categories ) ) {
echo '<a href="' . esc_url( get_category_link( $categories[0]->term_id ) ) . '">' . esc_html( $categories[0]->name ) . '</a>';
};
?>
get_the_tags() example:
<?php
$post_tags = get_the_tags();
if ( $post_tags ) {
foreach( $post_tags as $tag ) {
echo $tag->name . ', ';
}
}
?>