Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer 1

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;

became

remove_filter( 'the_content', 'display_post_taxonomies' );
return $content;
share|improve this answer
    
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. –  G. M. 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? –  G. M. Nov 11 '13 at 18:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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