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:
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
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:
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 thenget_the_terms()
should returnfalse
in which case nothing would be output.So – it seems that it’s up to us to filter the results, as follows:
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:
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
.