WordPress   

Get categories of the current post in WordPress

<?php 
$categories = get_the_category();
$separator = ' ';
$result = '';
if ( ! empty( $categories ) ) {
	foreach( $categories as $category ) {
		$result .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" alt="' . esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ) . '">' . esc_html( $category->name ) . '</a>' . $separator;
	}
	echo trim( $result, $separator );
}
?>
Need Professional Support for Your Website Problems?

Whether you're facing website issues or struggling with code implementation, our team is here to assist. Hire us to get your website back on track.

Hire Us