wordpress get post categories as array – get_the_category

3.33K views
no comments
4 Dec 2016 2:58 pm

the_category(); function is not allowing to direct change tags and attributes, get_the_category(); function return categories of posts in array so we can modify with custom html tags and attributes check out the below example code, its used in under loop of  #the_loop.

//Get categories $the_postid;
$categories = get_the_category($the_postid);
if(is_array($categories)){
   	echo '<ul class="col-md-8 post-categories nlrp">';
	foreach($categories as $postcat){
		$category_id = get_cat_ID( $postcat->name );
		$category_link = get_category_link( $category_id);
		echo '<li><a rel="category" href="'.esc_url($category_link).'">'.$postcat->name.'</a></li>';
	}
	echo '</ul>';
}

NOTE:Your Email Address will be not shown and please do not add spamming comments because here is REL="NOFOLLOW" on your links and comments also moderated shown.
<code>Put html css or any language code under this tag</code>