How to get a list of term names of the custom post type im currently on in single.php

SIDENOTE: This function can be used even if have posts belongs to one taxonomy only. You don’t need to have a post that belongs to two or more taxonomies within the same hierarchy. I have made this function very flexible

It is always a problem for me to display a category list or term list for a post if the following conditions occur

  • Having a post having terms that belongs to two different taxonomies

  • Using one template to display posts, and you have posts that have terms belonging to more that one taxonomy

You come to a situation where you need to duplicate functions, one for each taxonomy. For instance, a post belongs to terms in two taxonomies, mytax1 and mytax2. To display the term list, you have to do

echo get_the_term_list( $post->ID, 'mytax1' );
echo get_the_term_list( $post->ID, 'mytax2' );

This become a mess when the next post belongs to the default taxonomy category

I went and wrote a function that combines all in one, one function to display all terms of all taxonomies belonging to a post, and this function can be used to display any taxonomy of the build in taxonomies (except post_format)

Here is how the function works:

1.) This function makes use of get_the_term_list() to display the term list, so this function uses the same functionality as get_the_term_list()

2.) Must be used inside the loop. This function will not work outside the loop

3.) The post ID and taxonomy name are passed by default to the get_the_term_list() function

4.) Here is the list of the arguments

before (string)(optional) Leading text

Default: empty string

sep (string)(optional) String to separate tags

Default: empty string

after (string)(optional) Trailing text

Default: empty string

display_tax_name (bool)(optional) Should the taxonomy name be displayed in front of the list.

Default: false

taxonomy_sep (string)(optional) Text used to separate the taxonomy name from the term list

Default: ':    '

multi_tax_sep (string)(optional) If the post have terms that belongs to more than one taxonomy, the text used to separate the two or more term lists

Default: </br>

hierarchical (bool)(optional) Should the list display hierarchical taxonomies (like categories ) or non-hierarchical taxonomies (like post tags)

Default: true

5.) The arguments can be passed as an array or as a string to the function

6.) Needs PHP 5.4+

This will work (string)

'display_tax_name=" . true ."&hierarchical=" . false . "&taxonomy_sep=' . html_entity_decode( '&raquo;&nbsp;&nbsp;' )

And this will work

$args = [
    'display_tax_name' => true,
    'hierarchical' => false,
    'taxonomy_sep' => html_entity_decode( '&raquo;&nbsp;&nbsp;' ) 
] 

The function

function get_taxonomies_terms_links( $args="" ){
    global $post;
    
    $defaults = [
        'before'            => '', 
        'sep'               => '', 
        'after'             => '',
        'display_tax_name'  => false,
        'taxonomy_sep'      => '&colon; &nbsp; &nbsp;',
        'multi_tax_sep'     => '</br>',
        'hierarchical'      => true
    ];
    $args = wp_parse_args( $args, $defaults );

    $post_type = $post->post_type;
    $taxonomies = get_object_taxonomies( $post_type, 'objects' );

    $returned_list = [];
    foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
        if( $args['hierarchical'] == $taxonomy->hierarchical && has_term( '', $taxonomy_slug ) && 'post_format' != $taxonomy_slug ) {
            $term_list = get_the_term_list( $post->ID, $taxonomy_slug, $args['before'], $args['sep'], $args['after'] );
            
            if( true == $args['display_tax_name'] ){
                $returned_list[] = strtoupper($taxonomy_slug) . $args['taxonomy_sep'] . $term_list;
            }else{
                $returned_list[] = $term_list;
            }
        }
    }

    if( $returned_list ) {  
        $count =  count($returned_list); 
        if( 1 === $count ) {
            return implode( '', $returned_list );
        }else{
            $multi_list = [];
            foreach ( $returned_list as $key=>$value ) {
                if (array_key_exists($key + 1, $returned_list)) {
                    $multi_list[] = $value . $args['multi_tax_sep'];
                }else{
                    $multi_list[] = $value;
                }
            }
            return implode( '', $multi_list );
        }
    }
}

Example of use in a template to display the term list. You can either make use of the normal reading characters or HTML entities in conjunction with html_entity_decode() and the character chart

Arguments as string

<span class="cat-links">
    <?php echo get_taxonomies_terms_links('sep=, &display_tax_name=" . true ."&taxonomy_sep=' . html_entity_decode( '&raquo;&nbsp;&nbsp;' ) ); ?>
</span>

Arguments as an array

<span class="cat-links">
    <?php 
        echo get_taxonomies_terms_links(
            [
            'sep'               => ', ',
            'display_tax_name'  => true,
            'taxonomy_sep'      => html_entity_decode( '&raquo;&nbsp;&nbsp;' ) 
            ],
        );
    ?>
</span>

Leave a Comment