top of page

Get Posts Category name outside of a Loop

  • Writer: سُلَيْمَان بْن دَاوُوْد
    سُلَيْمَان بْن دَاوُوْد
  • Jun 9, 2020
  • 1 min read



The following pulls all category from a custom taxonomy that has a slug as 'product-type'

function getcategoryname_forproduct($postid){
	$categorys = get_the_terms($postid,'product-type'); 
	foreach ($categorys as $category) {
		$categor .= $category->name.', '; 
	}
	return rtrim($categor, ', '); 
}

The following pulls all categories a post belongs to default post


function getcategorynameallinpost($postid){
	$categorys = get_the_category($postid); 
	foreach ($categorys as $category) {
		$categor .= $category->name.', '; 
	}
	return rtrim($categor, ', '); 
}

Following Pulls all the tags (Custom Taxonomy in this case) of the post (custom post type in this case)


<?php 
$tags =  get_the_terms( get_the_ID(), 'content-type');
?>
<div class="link-sec">
   <?php
     if(is_array($tags)){
         foreach ($tags as $tag) {      ?>
            <a href="<?php echo get_term_link($tag->term_id,'content-type') ?>"><?php echo strtoupper($tag->name); ?></a>
  <?php      }
    }
?>


Recent Posts

See All
Hide Upcoming Events

Hiding Upcoming Events can be relatively useful when you are using ACF fields to show Dates for an an Event The Following Snippet is an...

 
 
 

Comments


© 2020 by syednazrulhassan

bottom of page