Change the body class name for a WordPress template page

Article

When you create a template page like “template-about.php” in your theme, WordPress add this ugly class name page-template-template-about-php in the body. So we’ll edit this.

Open the “functions.php” and add this following code for renaming page-template-template-about-php to page-template-about.

// Rename '.page-template-template-name-php' to '.page-template-name'.

function rename_template_body_class( $classes ) {
    foreach ( $classes as $k =>  $v ) {
        if ( substr($v, 0, 22) == 'page-template-template' )
            $classes[ $k ] = 'page-' . substr( $v, 14, -4 );
    }
    return $classes;
}
add_filter( 'body_class', 'rename_template_body_class' );
If you like (or not) this article, you can write a comment 0 or a tweet 4.

Discussion No comment - RSS feed

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>