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

css - Add styles to wordpress admin on the Page List

programmeradmin4浏览0评论

Is it possible to add the page slug as a class to the list of pages in Wordpress admin?

The reason is, I want to make programmatically hide pages in the admin if an option page exist for that page.

So my admin styles in function will be something like this:

#the-list .contact { display: none; }

instead of:

#the-list #post-20 { display: none; }

Screenshot attached.

Thank you

Is it possible to add the page slug as a class to the list of pages in Wordpress admin?

The reason is, I want to make programmatically hide pages in the admin if an option page exist for that page.

So my admin styles in function will be something like this:

#the-list .contact { display: none; }

instead of:

#the-list #post-20 { display: none; }

Screenshot attached.

Thank you

Share Improve this question edited Jun 5, 2019 at 15:13 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Jun 5, 2019 at 14:33 Schalk JoubertSchalk Joubert 1331 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

If you want to go with PHP solution, then the post_class filter hook (codex) would allow you to do that - see the example below. You can paste that to your functions.php.

Just be aware that hiding the post from posts list doesn't make it restricted from access, users can still use the post's edit url directly to access the edit screen or use the link in the top admin bar.

function set_admin_post_class( $classes, $class, $post_id ) {

    // affect post classes only in dashboard context
    if ( ! is_admin() ) {
         return $classes;
    }

    // Figure out where in dashboard we actually are
    // This will return early if we are not currently editing "pages"
    $screen = get_current_screen();
    if ( empty( $screen->post_type ) || 'page' !== $screen->post_type || 'edit' !== $screen->base ) {
        return $classes;
    }

    // Now let's get list of all items from admin menu
    global $menu;
    // $menu is an array of arrays, label comes always first in every element
    $menu_labels = array_map( function( $item ) {
        return array_shift( $item );
    }, $menu );

    // Finally we can check if a post name is the same as any of menu item's label
    $post_title = get_the_title( $post_id );
    if ( in_array( get_the_title( $post_id ), $menu_labels ) ) {
        $classes[] = sanitize_title_with_dashes( $post_title ); // you can set whatever class you want instead of $post_title. i.e. 'hidden' or 'contact'. In my example we are setting the same class as post title.
    }

    return $classes;
}

add_filter( 'post_class', 'set_admin_post_class', 10, 3 );
发布评论

评论列表(0)

  1. 暂无评论