This morning I needed to add a body class to a page only if a meta value stored in the posts meta data was a certain value. It’s easy enough to do with the body_classfilter and a couple of checks.
First, let’s see a basic example. To add a body class when the current post’s custom field value exists, do this:
Easy enough. But for my example, I wanted to only add the body class if the meta had a certain value, and I only wanted to add for pages, and I wanted to add the body class if either the current page or the current page’s parent had the meta value.
This is the first time I can remember where I checked for meta data when using the body_class filter, rather than just traditional conditionals, so I thought I’d share.