add featured image thumbnail to WordPress admin columns

Add featured image thumbnail to admin columns list for posts and pages:


<?php

// add featured image thumbnails to WordPress admin columns

// add_theme_support( 'post-thumbnails' ); // theme should support

function themename_add_post_thumbnail_column( $cols ) { // add the thumb column

	// output feature thumb in the end

	//$cols['themename_post_thumb'] = __( 'Featured image', 'themename' );

	//return $cols;

	// output feature thumb in a different column position

	$cols_start = array_slice( $cols, 0, 2, true );

	$cols_end   = array_slice( $cols, 2, null, true );

	$custom_cols = array_merge(

		$cols_start,

		array( 'themename_post_thumb' => __( 'Featured image', 'themename' ) ),

		$cols_end

	);

	return $custom_cols;

}

add_filter( 'manage_posts_columns', 'themename_add_post_thumbnail_column', 5 ); // add the thumb column to posts

add_filter( 'manage_pages_columns', 'themename_add_post_thumbnail_column', 5 ); // add the thumb column to pages



function themename_display_post_thumbnail_column( $col, $id ) { // output featured image thumbnail

	switch( $col ){

		case 'themename_post_thumb':

			if( function_exists( 'the_post_thumbnail' ) ) {

				echo the_post_thumbnail( 'thumbnail' );

			} else {

				echo __( 'Not supported in theme', 'themename' );

			}

			break;

	}

}

add_action( 'manage_posts_custom_column', 'themename_display_post_thumbnail_column', 5, 2 ); // add the thumb to posts

add_action( 'manage_pages_custom_column', 'themename_display_post_thumbnail_column', 5, 2 ); // add the thumb to pages

?>

Leave a Comment