I'm using the following code to display my custom post taxonomies:

function display_post_taxonomies( $content ) {

if( is_single() ) { 
    $args = array( 'public' => true, '_builtin' => false );     
    $output = 'objects';        
    $operator = 'and';      
    $taxonomies = get_taxonomies( $args, $output, $operator );

    if( $taxonomies ) {     
        $content .= '<div class="taxonomy_container">';         
        foreach( $taxonomies as $taxonomy ) {           
            $args = array(
                            'orderby'               => 'name',
                            'echo'                  => false,
                            'taxonomy'              => $taxonomy->name,
                            'title_li'              => '<span class="taxonomy_title">' . __( $taxonomy->labels->name, 'your-themes-text-domain' ) . '</span>',
                            'show_option_none'      => __( 'No ' . $taxonomy->labels->name, 'your-themes-text-domain' )
            $content .= '<ul>' . wp_list_categories( $args ) . '</ul>';             
        $content .= '</div>';       
return $content;

add_filter( 'the_content', 'display_post_taxonomies' );

This works fine, except there are some cases where I don't have anything in my custom taxonomy. In those cases, I don't want there to be anything displayed at all, i.e., No taxonomy title or default 'no terms' message.

I can get rid of the 'no terms' message by simply setting show_option_none to false, but that leaves the name of the taxonomy there.

What I really need is an if statement similar to

if($taxonomy => 'objects'){
    //$args & &content arrays

but that doesn't work.

Help much appreciated.


What you need is set the show_option_none to a string easily recognizable and then add the result of wp_list_categories to the content only if it it does not contain that string..

So your foreach loop shoul look somethinlg like

foreach( $taxonomies as $taxonomy ) {           
  $args = array(
    'orderby' => 'name',
    'echo' => false,
    'taxonomy' => $taxonomy->name,
    'title_li' => '<span class="taxonomy_title">' . __( $taxonomy->labels->name, 'your-themes-text-domain' ) . '</span>',
    'show_option_none' => '%%NOCAT%%'
  $list = wp_list_categories( $args );
  $empty = (bool) substr_count( strip_tags($list), '%%NOCAT%%');
  $content .= ! $empty ? '<ul>' . $list . '</ul>' : '';              

In this way if your custom tax has no terms the result of wp_list_categories is not added to the html output.

Additional suggestions

Just few days ago I experienced an issue using something like

add_filter( 'the_content', 'display_post_taxonomies' );

because some SEO plugins use get_the_excerpt to fill the meta description in the <head> section of the page. Problem is that get_the_excerpt, when there is no manual excerpt for post, call wp_trim_excerpt and this function fires the_content hook. Result: you will find the list of your taxonomy in the head of your page: that's bad.

Also you should note that 'the_content' hook can be fired not only in main query, so if you have some secondary queries on sidebar or footer your function will be called again there: that's bad.

What I suggest is where you have

if( is_single() ) {

replace with

if( is_single() && did_action('loop_start') ) {

This prevent function run in the <head> section.

After that, remove the filter befor return the content, in this way you are sure the filter run once, so

return $content;


remove_filter( 'the_content', 'display_post_taxonomies' );
return $content;
  • This doesn't seem to have worked :/ – sanjaypoyzer Nov 11 '13 at 16:37
  • There is no change, it still shows the empty titles just as before. – sanjaypoyzer Nov 11 '13 at 17:17
  • @sanjaypoyzer edited the answer. Tested and worked for me. – gmazzap Nov 11 '13 at 17:37
  • Putting the did_action stuff just makes my custom taxonomy not show up at all. I don't have any SEO plugins installed and my taxonomy is not coming up in my head. – sanjaypoyzer Nov 11 '13 at 18:16
  • Sorry @sanjaypoyzer right hook is did_action('loop_start'). It doesn't hurt and if if future you want to add seo plugin you'll not have problems. Anyway, my solution worked? – gmazzap Nov 11 '13 at 18:23

Use get_the_term_list instead of the_terms which return the terms list instead of printing, so output can be used for both purpose. Check the answer: Display taxonomy term only if there's a value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.