Add a post's taxonomy field value to that post's widget/front end

Hi

I have created a ‘project’ custom post type with two taxonomies: ‘client’ and ‘project_location’. I would like to display the project’s client and location in the sidebar of the project frontend.

I’m guessing I can add a tangible template shortcode to the projects’ widget area but I’m not sure what the loop should be to display the taxonomy field value of the current post. I can’t find anything in the documentation.

Can anyone advise please?

many thanks in advance

Hi @mjackson, the easiest way to do this is probably like this using the Taxonomy tag:

<Taxonomy project_location>
  <Term title />
</Taxonomy>

This is essentially just a shortcut to creating a taxonomy term loop and it’s the same as writing:

<Loop type=taxonomy_term taxonomy=project_location post=current>
  <Field title />
</Loop>

The reason this is necessary is that in WordPress, the data about which taxonomy terms are associated with which posts aren’t actually saved within the post itself. That’s why if you look through the list of fields available on a post loop, you won’t find anything related to taxonomy terms. Instead, this data is saved alongside the taxonomy term itself. If you check out the fields available on a taxonomy term loop you’ll see that there’s one called posts which is a field that contains all the posts that belong to the current taxonomy term. This isn’t a quirk of L&L, this is just down to where WordPress itself has decided to store this data.

All that to say that if you’re on a post, you can’t just grab terms from the current post and you instead need to create a taxonomy term loop where you’re saying “get me a list of all the taxonomy terms that have the current post ID in their posts field.” And then for each taxonomy term in that loop, you can display its title or any other field associated with it.

That’s genius! It worked a treat (both versions) - thank you so much Benjamin.

1 Like