最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Add term of current custom post type to admin body class using admin_body_class

programmeradmin0浏览0评论

I am trying to add the current custom post type term to the body class of my WordPress admin page. So when I am viewing an existing custom post type that has been assigned a term it will add that term to the body class.

I have found the following code but cannot get it to work for me:

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

    global $post;
    $terms = wp_get_post_terms( $post->ID, 'product_cat', array( 'fields' => 'all' ) );
    $terms = wp_list_pluck( $terms, 'slug' );
    foreach ( $terms as $term )
    {
        $classes .= ' my_taxonomy-' . $term;
    }

    return $classes;
}

Any pointers on how to get this working?

I am trying to add the current custom post type term to the body class of my WordPress admin page. So when I am viewing an existing custom post type that has been assigned a term it will add that term to the body class.

I have found the following code but cannot get it to work for me:

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

    global $post;
    $terms = wp_get_post_terms( $post->ID, 'product_cat', array( 'fields' => 'all' ) );
    $terms = wp_list_pluck( $terms, 'slug' );
    foreach ( $terms as $term )
    {
        $classes .= ' my_taxonomy-' . $term;
    }

    return $classes;
}

Any pointers on how to get this working?

Share Improve this question asked Dec 31, 2020 at 20:20 moorewebxmoorewebx 571 gold badge2 silver badges6 bronze badges 1
  • 1 It doesn't look like you're setting $screen anywhere, so $screen->base won't exist, so your code will exit early. – Pat J Commented Dec 31, 2020 at 21:25
Add a comment  | 

2 Answers 2

Reset to default 2

I think you're missing get_current_screen().

add_filter( 'admin_body_class', 'rw_admin_body_class' );
function rw_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' ) );
    $terms = wp_list_pluck( $terms, 'slug' );
    foreach ( $terms as $term )
    {
        $classes .= ' my_taxonomy-' . $term;
    }

    return $classes;
}

You can add custom-classe to the your wordpress custom admin page body classes like the following

$wpdocs_admin_page = add_options_page(__('Wpdocs Admin Page', 'wpdocs_textdomain'),
    __('Wpdocs Admin Page', 'wpdocs_textdomain'),
    'manage_options', 'wpdocs_textdomain', 'wpdocs_admin_page');

add_filter( 'admin_body_class', 'rw_admin_body_class' );
function rw_admin_body_class( $classes )
{
$screen = get_current_screen();
    if ( wpdocs_admin_page == $screen->id)
        return $classes;

    global $post;
    $terms = wp_get_post_terms( $post->ID, 'product_cat', array( 'fields' => 'all' ) );
    $terms = wp_list_pluck( $terms, 'slug' );
    foreach ( $terms as $term )
    {
        $classes .= ' my_taxonomy-' . $term;
    }

    return $classes;
}
发布评论

评论列表(0)

  1. 暂无评论