WordPress Support Plans

If a WordPress post has terms from a custom taxonomy

By David Foreman Updated Tags:

If you use custom post types, you’ll often have the need to do something if the current post has a specific term from a custom taxonomy

TL;DR: You can run code only when the current post has a specific term from a custom taxonomy with a short conditional snippet. It's ideal for custom post type templates; if you need lots of terms, WordPress's built-in categories are easier to manage.

You can accomplish this using the following:

All you need to do here is change the TERM to the name of the category within the custom taxonomy (exactly what you’ve called it, include spaces etc) and then add the name of the custom taxonomy.

An example:

The above snippet will then only echo out the code when the term is present in the current post.

When might this be handy?

This little snippet is best used if:

  • You want to use a custom post type template to be used with a custom taxonomy and have certain content displayed depending on the current custom term (within your custom taxonomy)
  • You can do all kind of things with this – our requirement was to show a specific WordPress menu when a particular term was used so we could display a menu of related content

If you are planning to have lots of terms (categories) we would suggest using the built-in WordPress categories, as these are a lot easier to work with.

Don’t forget that we offer WordPress support for any issue you have with WordPress. If you need help, just get in touch.

David Foreman

David Foreman

David is Toast’s Managing Director and co-founded the agency in 1997. He’s responsible for the commercial side and direction of the business, but is still hands-on with client WordPress projects. A graphic design graduate of Brunel University, he moved Toast over to WordPress around 2008 and now works in bespoke WordPress development and SEO. He’s straight-talking and focused on helping clients grow their businesses, not just making things look nice. Outside work, he enjoys motorbikes, blogging and doodling.

Get Help with Your WordPress Site

We’ve helped 100s of people to fix, improve, or even rebuild their WordPress websites. Call us on 01295 266644 or complete the form and we’ll contact you.

You can also drop us an email at hello@toastsupport.co.uk.