This page looks best with JavaScript enabled

how to display post based on a taxonomy term as get the term

 ·  ☕ 2 min read  ·  👽 john hashim

First things first you will have to get started this will work if you’re using CPT ( custom post type) In your theme or plugin development where you have a different term in your taxonomy.
lets you have this layout

  • Custom Post = sports
  • Taxonomy = types of sports
  • term = soccer, NFL, and golf.

maybe you want a different header on each term or you want each section to have its own posts depending on the taxonomy term.
this can be achieved by this method.

create a variable for your term arrays

1
2
3
<?php
$sport_term = get_the_terms(get_the_ID(), 'taxonamy-name');
?>

then you will loop your terms and use if statement in the loop to display specific terms depending on your wish … like this

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<?php
 foreach ( $sport_term as $term ) { ?>
  <?php if( $term->name  == 'nfl' ) : ?>
   this will display NFL if the term is  NFL!
   
  <?php elseif ( $term->name  == 'soccer' ) : ?>
             this will display soccer if term is soccer 
  <?php else : ?>
                it will default itself 
  <?php endif; ?>
  <?php
}
?>

Good luck with your development and please if this doesn’t work for your.. comment below what code worked for you.

Share on

john hashim
WRITTEN BY
john hashim
Web Developer