...
Tutorials, Wordpress

Get posts having multiple categories in WordPress

25 March 2019

If you want to show posts which have some specific categories for example have categories(Html, WordPress, css). You can assign multiple categories per post.

For Example:
<?php
 $categories = get_the_category($postid);
 $totalc = count($categories);
 if (!empty($categories)) {
foreach ($categories as $key => $t1) {
  if ($key == $totalc - 1) {
    $cname = $t1->name;
  } else {
     $cname = $t1->name . ',';
  }
 ?>
<a href="<?php echo get_term_link($t1); ?>" class="default-color"><?php echo $cname; ?></a> 
 <?php
 }
}
 ?>
Output here:

Multiple categories

Leave a Comments