css - Add styles to wordpress admin on the Page List

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 pro

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 );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745435126a4627573.html

相关推荐

  • css - Add styles to wordpress admin on the Page List

    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 pro

    10小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信