Add custom parameters into the body_class() function

Simply paste the following code into your functions.php file.

function custom_body_class($classes){
	if(is_singular()){
		$myvar = "myparam";
	} else {
		$myvar = "anotherparam";
	}

	global $post;
	array_push($classes, $myvar);
	return $classes;
}

add_filter('body_class', 'custom_body_class');

Of course, feel free to adapt the code to fit your needs. For example, a great use can be in multi-language sites: The custom parameter can be used to output the current language in the body class.

Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!

Add custom parameters into the body_class() function

Leave a Reply

Your email address will not be published. Required fields are marked *