Add CSS Page Name Classes to Your WordPress Body Tag

Super handy WordPress filter that adds your page names as a CSS class to the WordPress body_class function. Add the following to functions.php….filter-licious.

<?php
add_filter('body_class','page_class');
function page_class($classes) {
    global $wp_query;
    $page = '';
    $page = $wp_query->query_vars['pagename'];
    // add 'pagename' to the $classes array
    $classes[] = $page;
    // return the $classes array
    return $classes;
}

Leave a Reply

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

Are you in need of a freelance web developer?

Hire me