0

So why are category classes not added to the admin body class with this function?

I see the current post ID in page source as part of the complete body class, so the basic function of adding body classes to admin with admin_body_class does work.

Any ideas?

function base_admin_body_class( $classes )
{
    // Current action
    if ( is_admin() && isset($_GET['action']) ) {
        $classes .= 'action-'.$_GET['action'];
    }
    // Current post ID
    if ( is_admin() && isset($_GET['post']) ) {
        $classes .= ' ';
        $classes .= 'post-'.$_GET['post'];
    }

   // Get Categories
    if ( is_admin() && isset($_GET['post']) ) {

    $cats = wp_get_post_categories( $post->ID, array( 'fields' => 'all' ) );
    $cats = wp_list_pluck( $cats, 'slug' );
    foreach ( $cats as $cat )
    {
        $classes .= ' category-' . $cat;
    }
}

    return $classes;
}
add_filter('admin_body_class', 'base_admin_body_class');

1 Answer 1

2

You're using $post->ID which is undefined. Try $_GET['post'] instead:

// Get Categories
if ( is_admin() && isset($_GET['post']) ) {
    $cats = wp_get_post_categories( (int)$_GET['post'], array( 'fields' => 'all' ) );
    $cats = wp_list_pluck( $cats, 'slug' );
    foreach ( $cats as $cat ) {
        $classes .= ' category-' . $cat;
    }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.