Adding a column with a thumbnail to the WordPress Post List

To add a column with a thumbnail entry to the table of the list of entries in the WordPress admin area, add the following code:

add_filter('manage_posts_columns', 'posts_columns', 5);
add_action('manage_posts_custom_column', 'posts_custom_columns', 5, 2);

function posts_columns($defaults){
    $cb = $defaults['cb'];
    unset($defaults['cb']);
    $add = array('cb' => $cb, 'thumbnail_column' => 'Миниатюра');
    $defaults = $add + $defaults;
    return $defaults;
}

function posts_custom_columns($column_name, $id){
    if($column_name === 'thumbnail_column'){
        the_post_thumbnail('thumbnail', array('width' => 60) );
    }
}

Adding thumbnails for pages:

add_filter('manage_page_posts_columns', 'posts_columns', 5);
add_action('manage_page_posts_custom_column', 'posts_custom_columns', 5, 2);

Adding thumbnails for a custom post type:

add_filter('manage_post-type-name_posts_columns', 'posts_columns', 5);
add_action('manage_write-type-name_posts_custom_column', 'posts_custom_columns', 5, 2);

Let's make a little the width of the column with a thumbnail:

add_action('admin_head', 'column_style');

function column_style(){
    echo '<style>#thumbnail_column{
    width: 100px;
} </style>';
}

Updated: 01.05.2019

Did this article help you? Rate it!
(1 votes, average: 3.00 out of 5)
Loading...


Leave a Reply

Your email address will not be published. Required fields are marked *