admin管理员组

文章数量:1332361

I use the below code . But the code display Thumbnail in the five number column. How to move it first column position?

//show Thumbnail in dashboard
function my_function_admin_bar(){ return false; }
add_filter( 'show_admin_bar' , 'my_function_admin_bar');

add_image_size( 'admin-list-thumb', 80, 80, false );

add_filter('manage_posts_columns', 'tcb_add_post_thumbnail_column', 1);
add_filter('manage_pages_columns', 'tcb_add_post_thumbnail_column', 1);

function tcb_add_post_thumbnail_column($cols){
$cols['tcb_post_thumb'] = __('Thumbnail');
return $cols;
}

add_action('manage_posts_custom_column', 'tcb_display_post_thumbnail_column', 1, 2);
add_action('manage_pages_custom_column', 'tcb_display_post_thumbnail_column', 1, 2);

function tcb_display_post_thumbnail_column($col, $id){
switch($col){
case 'tcb_post_thumb':
if( function_exists('the_post_thumbnail') )
echo the_post_thumbnail( 'admin-list-thumb' );
else
echo 'Not supported in theme';
break;
}
}

I use the below code . But the code display Thumbnail in the five number column. How to move it first column position?

//show Thumbnail in dashboard
function my_function_admin_bar(){ return false; }
add_filter( 'show_admin_bar' , 'my_function_admin_bar');

add_image_size( 'admin-list-thumb', 80, 80, false );

add_filter('manage_posts_columns', 'tcb_add_post_thumbnail_column', 1);
add_filter('manage_pages_columns', 'tcb_add_post_thumbnail_column', 1);

function tcb_add_post_thumbnail_column($cols){
$cols['tcb_post_thumb'] = __('Thumbnail');
return $cols;
}

add_action('manage_posts_custom_column', 'tcb_display_post_thumbnail_column', 1, 2);
add_action('manage_pages_custom_column', 'tcb_display_post_thumbnail_column', 1, 2);

function tcb_display_post_thumbnail_column($col, $id){
switch($col){
case 'tcb_post_thumb':
if( function_exists('the_post_thumbnail') )
echo the_post_thumbnail( 'admin-list-thumb' );
else
echo 'Not supported in theme';
break;
}
}
Share Improve this question edited Sep 24, 2015 at 7:57 Aravona 5851 gold badge5 silver badges19 bronze badges asked Sep 24, 2015 at 6:15 Nasim FirozNasim Firoz 231 silver badge4 bronze badges 1
  • Do you read this post - wpengineer/1960/display-post-thumbnail-post-page-overview ? – bueltge Commented Sep 24, 2015 at 18:15
Add a comment  | 

2 Answers 2

Reset to default 4

You can add featured image thumbnail in post column with this code.

Copied from here Add Featured Post Thumbnails to WordPress Admin Post Columns. Haven't tried it myself but it must work.

add_image_size( 'admin-list-thumb', 80, 80, false );

// add featured thumbnail to admin post columns
function wpcs_add_thumbnail_columns( $columns ) {
    $columns = array(
        'cb' => '<input type="checkbox" />',
        'featured_thumb' => 'Thumbnail',
        'title' => 'Title',
        'author' => 'Author',
        'categories' => 'Categories',
        'tags' => 'Tags',
        'comments' => '<span class="vers"><div title="Comments" class="comment-grey-bubble"></div></span>',
        'date' => 'Date'
    );
    return $columns;
}

function wpcs_add_thumbnail_columns_data( $column, $post_id ) {
    switch ( $column ) {
    case 'featured_thumb':
        echo '<a href="' . get_edit_post_link() . '">';
        echo the_post_thumbnail( 'admin-list-thumb' );
        echo '</a>';
        break;
    }
}

if ( function_exists( 'add_theme_support' ) ) {
    add_filter( 'manage_posts_columns' , 'wpcs_add_thumbnail_columns' );
    add_action( 'manage_posts_custom_column' , 'wpcs_add_thumbnail_columns_data', 10, 2 );
    add_filter( 'manage_pages_columns' , 'wpcs_add_thumbnail_columns' );
    add_action( 'manage_pages_custom_column' , 'wpcs_add_thumbnail_columns_data', 10, 2 );
}

EDIT

Just tried it and it's working fine.

Although it works fine out of the box but you can change thumbnail size in above code if you want to use your custom defined image size.

EDIT

Just fixed this code for 80x80 thumbnail.

Here's a slightly cleaner version of the code posted by Nick. A couple of differences:

  • I'm using anonymous functions to avoid cluttering the global namespace. Note that if you only need to add the code to a single post type, you can also use a closure:
add_filter('manage_posts_columns', function() {
    // Do your thing here
});
  • Instead of returning a completely new $columns array, i'm adding the column in the existing array. If another column is added (either by a WordPress update or another piece of code), this method won't break that.
  • A simple if in the manage_posts_custom_column hook instead of a switch seems to make more sense here.
add_image_size("admin-list-thumb", 80, 80, false);

$cols_fn = function($cols) {
    $col_position = 1; // Change this to another position if you want

    return array_merge(
        array_splice($cols, 0, $col_position),
        ["admin-thumb" => "Thumb"],
        $cols
    );
};

$custom_cols_fn = function($col, $id) {
    if ($col == "admin-thumb") {
        $link = get_edit_post_link();
        $thumb = get_the_post_thumbnail($id, "admin-list-thumb");
        echo $thumb ? "<a href='$link'>$thumb</a>" : "—";
    }
};

add_filter('manage_posts_columns', $cols_fn);
add_action('manage_posts_custom_column', $custom_cols_fn, 10, 2 );
add_filter('manage_pages_columns', $cols_fn);
add_action('manage_pages_custom_column', $custom_cols_fn, 10, 2 );

本文标签: How to Display posts thumbnail in dashboard all posts row in first column