Daily Tips WordPress

How to: Add Thumbnails in Manage Posts/Pages List (WP_List_Table)

0
2 0

With today’s tutorial you will learn how to show post/custom post types thumbnails (featured image) on Manage Posts/Pages as known bydeveloper as WP_List_Table. Because, let’s be honest we all need this feature. It will be perfect if the WordPress community will integrate this feature directly into the core of WordPress :).

In the end the default List Table which by default looks like:

featured_image_posts_list_wordpress_normal

Will look like:

featured_image_posts_list_wordpress

 

So let’s start.

Step 1: Open up functions.php inside your theme’s directory!

Step 2: Go to the end of this opened file (functions.php) and copy this piece of code:

if ( !function_exists('stanleytips_featured_image_posts_list_column') && function_exists('add_theme_support') ) {

    // for post and page
    add_theme_support('post-thumbnails', array( 'post', 'page' ) );

    function stanleytips_featured_image_posts_list_column($cols) {
        $cols['thumbnail'] = __('Thumbnail');
        return $cols;
    }

    function stanleytips_featured_image_posts_list_value($column_name, $post_id) {

    	    //Width & Height of the featured image
            $width = (int) 50;
            $height = (int) 50;

            if ( 'thumbnail' == $column_name ) {
                // Featured image of the post
                $thumbnail_id = get_post_meta( $post_id, '_thumbnail_id', true );
                // Get image from Media
                $attachments = get_children( array('post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image') );
                if ($thumbnail_id){
                    $thumb = wp_get_attachment_image( $thumbnail_id, array($width, $height), true );
                } elseif ($attachments) {
                    foreach ( $attachments as $attachment_id => $attachment ) {
                        $thumb = wp_get_attachment_image( $attachment_id, array($width, $height), true );
                    }
                }

                if ( isset($thumb) && $thumb ) {
                    echo $thumb;
                } else {
                    echo __('None');
                }
            }
    }

    // These actions will add the featured image on posts list table
    add_filter( 'manage_posts_columns', 'stanleytips_featured_image_posts_list_column' );
    add_action( 'manage_posts_custom_column', 'stanleytips_featured_image_posts_list_value', 10, 2 );

    // These actions will add the featured image on pages list table
    add_filter( 'manage_pages_columns', 'stanleytips_featured_image_posts_list_column' );
    add_action( 'manage_pages_custom_column', 'stanleytips_featured_image_posts_list_value', 10, 2 );
}

Step 3(*): After saving the functions.php you don’t see anything changed on your Posts_Table, please do the following:

Final Note: Hope you enjoy this tutorial. If you have any suggestions for tutorials or lessons regarding WordPress, please don’t hesitate to leave a comment.

Please also share this article to spread the word.

Thank you

Stanley
I am a self made webmaster, blogger, web technology enthusiast and founder of StanleyTips.com.
You may also like
How to: Enable gzip Compression WordPress
Best WordPress Nonprofit and Political Themes of 2016

Leave Your Comment

Your Comment*

Your Name*
Your Webpage

58 + = 64

Enjoy This Article?

Join My Mailing List...

Facebook

Twitter

THANK YOU