Add a custom class/classes to body_class()
In WordPress, you can easily add a custom class to the body tag of your pages and posts using the body_class()
function. This function allows you to specify a list of classes that will be added to the body tag, giving you more control over the styling and layout of your content.
To use the body_class()
function, simply call it in your theme’s functions.php file, like so:
<?php
function custom_body_classes() {
return array('class-webninja-1', 'class-webninja-2');
}
add_filter('body_class', 'custom_body_classes');
?>
In this example, we’re defining a custom function that returns an array of two classes, 'class-webninja-1'
and 'class-webninja-2'
. We’re then adding this function to the body_class filter, which will apply these classes to the body tag of our pages and posts.
Or you can, for example, do not use an array:
<?php
function custom_body_class( $classes ) {
$classes[] = 'class-webninja';
return $classes;
}
add_filter( 'body_class', 'custom_body_class' );
?>
You can also use the body_class()
function in your template files to add custom classes to specific pages or posts. For example:
<body class="<?php body_class(); ?>">
<!-- Content here -->
</body>
By using the body_class()
function, you can easily add custom classes to your WordPress content, pages, and posts, giving you more flexibility and control over the design and layout of your site.