A better body-class function for WordPress

UPDATED– October 20, 2013: Thanks to Marty (see comments below), for convincing us to change out our method for body classes to filter the default WP body_class() function instead of creating our own. It still gives you all the advantages of our original function plus the classes you get by default.

Here’s what it does

The PHP

Add this stuff to your functions.php file, or just use our html5 shell and it’s built in.

The HTML

Your Help

Let us know if you see ways to make it better, probably more conditions could be added to the beginning to take into consideration more template types.

  • Marty Thornley

    You can add or remove any classes you want using the ‘body_class’ filter. I personally use the body classes all the time but if you want simpler, it’s easy to filter it to be whatever you want.

    return apply_filters( ‘body_class’, $classes, $class );

    http://core.trac.wordpress.org/browser/tags/3.5.1/wp-includes/post-template.php#L0

    • Brent Lagerman

      Thanks Marty, you’ve convinced me to retire our function in favor of a filter on the body_class function… My main gripe with it is that it returns SO much crap I don’t need and not some of the obvious stuff a themer would want… Thanks for pointing out this method though, it seems like a better way to do it.

Enable Javascript