How to Add Custom Body Class in Genesis Theme?

Genesis Framework : In this tutorial, you’ll learn how to Add Custom Body Class in Genesis. The Genesis Framework empowers you to quickly and easily build incredible websites with WordPress. Whether you’re a novice or advanced developer, Genesis provides the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go. In Genesis powered websites you’ve to modify the child theme which is running on.

These code snippets will help you to Add Custom Body Class with the Genesis Framework.

Unless otherwise indicated, the code snippets you see below should be placed into your theme’s functions.php file.

Add Custom Body Class in Genesis


Add body class to all pages on your site:
//* Do NOT include the opening php tag
//* Add custom body class to the head
add_filter( 'body_class', 'sp_body_class' );
function sp_body_class( $classes ) {
$classes[] = 'custom-class';
return $classes;
}

Add body class to a page with a slug of ‘sample-page’:
//* Do NOT include the opening php tag
//* Add custom body class to the head
add_filter( 'body_class', 'sp_body_class' );
function sp_body_class( $classes ) {
if ( is_page( 'sample-page' ) )
$classes[] = 'custom-class';
return $classes;
}

Add body class to a page with an ID of 1:
//* Do NOT include the opening php tag
//* Add custom body class to the head
add_filter( 'body_class', 'sp_body_class' );
function sp_body_class( $classes ) {
if ( is_page( '1' ) )
$classes[] = 'custom-class';
return $classes;
}

Add body class to a category page with a slug of ‘sample-category’:
//* Do NOT include the opening php tag
//* Add custom body class to the head
add_filter( 'body_class', 'sp_body_class' );
function sp_body_class( $classes ) {
if ( is_category( 'sample-category' ) )
$classes[] = 'custom-class';
return $classes;
}

Add body class to a category page with an ID of 1:
//* Do NOT include the opening php tag
//* Add custom body class to the head
add_filter( 'body_class', 'sp_body_class' );
function sp_body_class( $classes ) {
if ( is_category( '1' ) )
$classes[] = 'custom-class';
return $classes;
}

Add body class to a tag page with a slug of ‘sample-tag’:
//* Do NOT include the opening php tag
//* Add custom body class to the head
add_filter( 'body_class', 'sp_body_class' );
function sp_body_class( $classes ) {
if ( is_tag( 'sample-tag' ) )
$classes[] = 'custom-class';
return $classes;
}

Add body class to a tag page with an ID of 1:
//* Do NOT include the opening php tag
//* Add custom body class to the head
add_filter( 'body_class', 'sp_body_class' );
function sp_body_class( $classes ) {
if ( is_tag( '1' ) )
$classes[] = 'custom-class';
return $classes;
}

Final Words

I strongly recommended to take backup core files before customizing any Genesis Child Theme. Hope the above Genesis Tutorial was helpful and valued for you. Please don’t forget to share this with social friends and write a comment below if you need any assistance in customizing the look of your WordPress websites.


Editorial Note: When you buy through links on our site, we may earn an affiliate commission. Commissions do not affect our writers' opinions or evaluations.