Skip to content


Request to add taxonomy terms to body class #11

devinsays opened this Issue · 2 comments

2 participants


There is a request to add taxonomy body classes. Will need to look into how core does it for standard posts.

Here's a stack-exchange post about add the body classes:


@garyj Would you have a minute to eyeball this last commit?

If you don't see any issues I'm ready to upload this latest version to

@devinsays devinsays closed this

There's at least one issue (unprefixed function name), and a couple of potential improvements - but I won't be able to code up something until at least tonight. Leave it with me.

Edit: The plugin is of course a class, so the comment about unprefixed function name is incorrect.

@GaryJones GaryJones added a commit to GaryJones/portfolio-post-type that referenced this issue
@GaryJones GaryJones Re-work taxonomy term body classes.
Simplifies the functions down from three to one. Here, we re-specify the names of the taxonomies, but a future refactor should be able to pull these out into a class property that can be used in multiple methods (including registration).

Use of get_the_ID() instead of global $post; $post->ID.

Better error checking for what get_the_terms() can return, in case someone has unregistered the two taxonomies.

Use of sanitize_html_class() to ensure term values are appropriately sanitized for output.

Use of taxonomy (with underscore replaced with hyphen per code standards for class names) as prefix for the term class, so that there's far less possibility of a term clashing with existing body classes. e.g. a portfolio might be tagged with "full-width", but without a prefix, this might confuse the theme into thinking that the single page should be displayed full width. With this change, the class becomes "portfolio-tag-full-width".

Use of single instance of aray_unique() to remove any duplicates once the classes have been added, instead of a check for in_array() for each and every term for both taxonomies.

See #11.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.