Code Snippets

Add Custom Body Class To WordPress Template

This snippet can be added to the top of your page or post template php file to add a custom body class.

When creating a custom template for a page, it is useful to be able to give that template a custom body class.

This snippet can be added to the top of your page or post template php file to do that.

// Add custom body class to the head
add_filter( 'body_class', 'add_body_class' );
function add_body_class( $classes ) {
   $classes[] = 'name-of-css-class-goes-here';
   return $classes;
}

Hire a WordPress Expert

Get hourly and project based theme customization, theme tweaks, and development help from an experienced Kelowna WordPress developer.  We’ve been building WordPress websites full-time for over a decade. We can help you.