How to assign a class to a page with a custom template?

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, … Read more

Remove and add class with body_class() function

The my_custom_class() function is using the class name my-custom-class, but the CSS is targeting the class my_custom_class. These two strings should be exactly the same: .my-custom-class { margin-top: 350px !important; } Also, it would be a little cleaner to handle all of the body_class stuff in a single callback function: add_action( ‘body_class’, ‘my_custom_class’); function my_custom_class( … Read more

Adding a body class with ACF

Hook into the body_class filter and add your field there. It might be better to get the ID from get_queried_object_id() instead of get_the_ID(). add_filter( ‘body_class’, ‘wpse_20160118__body_class’ ); function wpse_20160118__body_class( $classes ) { if ( $package_colour = get_field( ‘package_colour’, get_queried_object_id() ) ) { $package_colour = esc_attr( trim( $package_colour ) ); $classes[] = $package_colour; } return $classes; … Read more

Add post class to the TinyMCE iframe?

The filter you’re after is tiny_mce_before_init. Using this, we can hook into TinyMCE’s ‘init_array’ and add body classes: add_filter( ‘tiny_mce_before_init’, ‘wpse_235194_tiny_mce_classes’ ); function wpse_235194_tiny_mce_classes( $init_array ){ global $post; if( is_a( $post, ‘WP_Post’ ) ){ $init_array[‘body_class’] .= ‘ ‘ . join( ‘ ‘, get_post_class( ”, $post->ID ) ); } return $init_array; } We’re joining the post … Read more