WordPress Custom Taxonomy – List them individually

My problem is that I want to list my custom taxonomies singularly rather than all on one line.

I have the parent taxonomies Location and Venue and would like to list them as such:

Read More

Location: example location

Venue: example venue

But the code i am using displays them as:

example location, example venue

The code I’m using is:

<?php  the_terms( $post->ID, 'uk_events' ,  ' '); ?>

uk_events is the custom post type that I am using, location and venue are the parent taxonomies in that post type.

Can anyone help with this, it would be very appreciated as I have not found any info on displaying taxonomies like this.

Thank you

Related posts

Leave a Reply

1 comment

  1. Firstly, I’m not sure you’re meant to pass the custom post type to the_terms().

    Regardless, I think get_the_terms() will give you the flexibility that you’re after.

    Eg:

    <?php
        $taxList = array("location", "venue");
        foreach ($taxList as $tax) {
            $terms = get_the_terms( $post->ID, $tax );      
            if ( $terms && ! is_wp_error( $terms ) ) {
                echo "<br/>{$tax}: ";
                $separator = "";
                foreach ( $terms as $term ) {
                    echo "{$separator}{$term->name}";
                    $separator ", ";
                }
            }
        }
    ?>
    

    NB this was typed out on an iPad so may contain syntax or logic errors as I can’t test… But think the idea should be sound enough to help!


    Update: OP advises that the above code is displaying all taxonomy terms on each line, which suggests the result from get_the_terms() is not filtered by the taxonomy specified at the second parameter.

    I wondered if this might be because the taxonomy names are case sensitive, and hence how I’ve specified them in the $taxList array may not be correct, but according to the docco, if this were the case then get_the_terms() should return false in which case nothing would be output.

    So – it seems that it’s up to us to filter the results, as follows:

    <?php
        $taxList = array("location", "venue");
        foreach ($taxList as $tax) {
            $terms = get_the_terms( $post->ID, $tax );      
            if ( $terms && ! is_wp_error( $terms ) ) {
                echo "<br/>{$tax}: ";
                $separator = "";
                foreach ( $terms as $term ) {
                    if (strtolower($term->taxonomy) == strtolower($tax)) {// <-- New (redundant!?) filter.
                        echo "{$separator}{$term->name}";
                        $separator ", ";
                    }
                }
            }
        }
    ?>
    

    Update: A further change might be to automagically pick up the taxonomies used in the current post to avoid typos etc, by updating the first line to this:

     $taxList = get_object_taxonomies('uk_events');
    

    And then further, this code can then be used for all post types (eg from a call in a template to a function with this code at functions.php) by replacing 'uk_events' with $post->post_type.