Was recently working on a new post layout for a site that we’re wanting to do some A/B testing for before shipping out the feature for the entire backlog of posts.
Once we implement, there may be a few posts that keep the old layout which we’ll be controlling by giving those posts a certain body class within the WordPress dashboard when editing those posts. However, there are some functions that are used on the page that we’d like to control based on the layout (ex. a related post function calling 3 posts rather than 4).
To do this, here is a quick conditional that you can add to your functions.php
file or a general use plugin that will allow you to check whether or not a class exists in the body class for the page.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Conditional to check whether or not the body class contains a given class. | |
* | |
* @param string $class The class we're lookging for in the body class. | |
*/ | |
function has_body_class( $class ) { | |
$body_classes = get_body_class(); | |
return in_array( $class, $body_classes, true ); | |
} |