Display list of categories that are related to a particular post on your single page

Marko Krstić
30 Dec 2019

<?php
	$categories = get_the_category();
		if($categories){
			echo '<div>';
			foreach($categories as $category) {
			echo '<span>' . $category->cat_name . '</span>';
		}
	echo '</div>';
}
?>

If you need just parent cagetory

Parrent

<?php
  $perma_cat = get_post_meta($post->ID , '_category_permalink', true);
  if ( $perma_cat != null ) {
    $cat_id = $perma_cat['category'];
    $category = get_category($cat_id);
  } else {
    $categories = get_the_category();
    $category = $categories[0];
  }
  $category_link = get_category_link($category);
  $category_name = $category->name;  
?>                                   
<a href="<?php echo $category_link ?>"><?php echo $category_name ?></a>

Was this article helpful?

Tags

Categories

Never miss new post again

Subscribe and get list of new posts in your inbox

Click to Copy