WordPress后台文章和页面列表添加自定义列

Author: 陌小雨Date: 2016-04-27View: 76

有的时候我们需要直观的知道文章或者页面的id是多少,也就是在文章和页面列表添加文章id的自定义列,下面的代码就很有用了:

/**********文章***********/  
    add_filter('manage_posts_columns', 'add_new_posts_columns');   
    function add_new_posts_columns($book_columns) {   
           
        $new_columns['cb'] = '<input type="checkbox" />';   
        $new_columns['id'] = __('ID');   
        $new_columns['title'] = _x( 'Title', 'column name' );   
        $new_columns['author'] = __('Author');   
        $new_columns['categories'] = __('Categories');   
        $new_columns['tags'] = __('Tags');   
        $new_columns['date'] = _x('Date', 'column name');   
        return $new_columns;   
    }   
    add_action('manage_posts_custom_column', 'manage_posts_columns', 10, 2);   
    function manage_posts_columns($column_name, $id) {   
        global $wpdb;   
        switch ($column_name) {   
        case 'id':   
            echo $id;   
            break;   
        default:   
            break;   
        }   
    }   
    /**************页面************/  
    add_filter('manage_pages_columns', 'add_new_pages_columns');   
    function add_new_pages_columns($book_columns) {   
           
        $new_columns['cb'] = '<input type="checkbox" />';   
        $new_columns['id'] = __('ID');   
        $new_columns['title'] = _x( 'Title', 'column name' );   
        $new_columns['author'] = __('Author');   
        $new_columns['date'] = _x('Date', 'column name');   
        return $new_columns;   
    }   
    add_action('manage_pages_custom_column', 'manage_pages_columns', 10, 2);   
    function manage_pages_columns($column_name, $id) {   
        global $wpdb;   
        switch ($column_name) {   
        case 'id':   
            echo $id;   
            break;   
        default:   
            break;   
        }   
    }

效果如下:

WordPress后台页面列表:

20160427123825

WordPress后台文章列表:

20160427123854

当然了,如果只是分享这个,就不是我陌小雨的风格了,陌小雨想带给大家的是一种思路:比如下面这些

20160427134250

到这里,我知道你想说什么了,什么也不说了,上图吧,^_^

nizhenjizhi

2016-7-8日更新:

用陌小雨上面分享的代码,id是显示在最左侧的,宽度也没法自定义,好处是你可以增加自定义字段的列,可操作性强。如果你觉得难看的话,可以使用下面的代码,可以让WordPress后台显示文章id、分类目录id、链接分类ID等。

//代码来源于: Show IDs by 99 Robots插件
    // Hooks + Filters
    add_action( 'admin_head', array('WPSiteShowIDs', 'add_css'));
    // For Post Management
    add_filter( 'manage_posts_columns', array('WPSiteShowIDs', 'add_column') );
    add_action( 'manage_posts_custom_column', array('WPSiteShowIDs', 'add_value'), 10, 2 );
    // For Page Management
    add_filter( 'manage_pages_columns', array('WPSiteShowIDs', 'add_column') );
    add_action( 'manage_pages_custom_column', array('WPSiteShowIDs', 'add_value'), 10, 2 );
    // For Media Management
    add_filter( 'manage_media_columns', array('WPSiteShowIDs', 'add_column') );
    add_action( 'manage_media_custom_column', array('WPSiteShowIDs', 'add_value'), 10, 2 );
    // For Link Management
    add_filter( 'manage_link-manager_columns', array('WPSiteShowIDs', 'add_column') );
    add_action( 'manage_link_custom_column', array('WPSiteShowIDs', 'add_value'), 10, 2 );
    // For Link_Category Management
    add_action( 'manage_edit-link_category_columns', array('WPSiteShowIDs', 'add_column') );
    add_filter( 'manage_link_category_custom_column', array('WPSiteShowIDs', 'add_return_value'), 10, 3 );
    // For Category Management
    add_action( 'manage_edit-link-categories_columns', array('WPSiteShowIDs', 'add_column') );
    add_filter( 'manage_link_categories_custom_column', array('WPSiteShowIDs', 'add_return_value'), 10, 3 );
    // For User Management
    add_action( 'manage_users_columns', array('WPSiteShowIDs', 'add_column') );
    add_filter( 'manage_users_custom_column', array('WPSiteShowIDs', 'add_return_value'), 10, 3 );
    // For Comment Management
    add_action( 'manage_edit-comments_columns', array('WPSiteShowIDs', 'add_column') );
    add_action( 'manage_comments_custom_column', array('WPSiteShowIDs', 'add_value'), 10, 2 );
    add_action('admin_init', array('WPSiteShowIDs', 'custom_objects'));
    class WPSiteShowIDs {
        static function custom_objects() {
            // For Custom Taxonomies
            foreach ( get_taxonomies(array('public'   => true), 'names') as $custom_taxonomy ) {
                if (isset($custom_taxonomy)) {
                    add_action("manage_edit-" . $custom_taxonomy . "_columns", array('WPSiteShowIDs', 'add_column'));
                    add_filter("manage_" . $custom_taxonomy . "_custom_column", array('WPSiteShowIDs', 'add_return_value'), 10, 3);
                }
            }
            // For Custom Post Types
            foreach (get_post_types(array('public'   => true ), 'names') as $post_type) {
                if (isset($post_type)) {
                    add_action("manage_edit-". $post_type . "_columns", array('WPSiteShowIDs', 'add_column'));
                    add_filter("manage_". $post_type . "_custom_column", array('WPSiteShowIDs', 'add_return_value'), 10, 3);
                }
            }
        }
        static function add_css() {
            ?>
            <style type="text/css">
                #wpsite-show-ids {
                    width: 50px;
                }
            </style>
            <?php
        }
        static function add_column($cols) {
            $cols['wpsite-show-ids'] = 'ID';
            return $cols;
        }
        static function add_value($column_name, $id) {
            if ( $column_name == 'wpsite-show-ids' ) {
                echo $id;
            }
        }
        static function add_return_value($value, $column_name, $id) {
            if ( $column_name == 'wpsite-show-ids' ) {
                $value = $id;
            }
            return $value;
        }
    }