Using is_page(8)
will make your code a bit static. Let’s make it dynamic as you’re after with is_page_template()
:
<?php
function prefix_conditional_body_class( $classes ) {
if( is_page_template('mytemplate.php') )
$classes[] = 'mytemplate';
return $classes;
}
add_filter( 'body_class', 'prefix_conditional_body_class' );
Worked for me in a child theme with the template file in the root of the child theme, using template twentyfifteen.
And I don’t know why you need another class to be added, if you are using body_class()
you will already get two classes there: page-template-mytemplate
and page-template-mytemplate-php
. They will be unique and you can use them.