Recently a project required me to make some CSS changes to the admin side of my posts in which a class was added to the body based on the current posts category. In this particular instance it need to be done to the WooCommerce products.

So if we were editing a product in the “Shoes” category it would add a body class containing that category name. To do this we use the following snippet:

 add_filter( 'admin_body_class', 'mwx_admin_body_class' );
function mwx_admin_body_class( $classes ) {
    $screen = get_current_screen();
    if ( 'post' != $screen->base ) {
        return $classes;

    global $post;
    $terms = wp_get_post_terms( $post->ID, 'product_cat', array( 'fields' => 'all' ) );  // 'product_cat' is the taxonomy 
    $terms = wp_list_pluck( $terms, 'slug' );
    foreach ( $terms as $term )
        $classes .= ' mwx_taxonomy-' . $term;  // ' mwx_taxonomy-' will prepend the class.  We put a space in front of it to prevent merging of other classes

    return $classes;

Leave a Reply

Get in Touch

Please fill out the form below and we will get back to you ASAP

  • This field is for validation purposes and should be left unchanged.