Confused ? How to show taxonomies terms of a posts into a wordpress page

Just add this code where you want to post categories with links

// your taxonomy name
$tax = 'post_tag';

// get the terms of taxonomy
$terms = get_terms( $tax, [
  'hide_empty' => false, // do not hide empty terms
]);

// loop through all terms
foreach( $terms as $term ) {

  // if no entries attached to the term
  if( 0 == $term->count )
    // display only the term name
    echo '<h4>' . $term->name . '</h4>';

  // if term has more than 0 entries
  elseif( $term->count > 0 )
    // display link to the term archive
    echo '<h4><a href="'. get_term_link( $term ) .'">'. $term->name .'</a></h4>';

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s