如何在仪表板中第一列显示所有帖子行的帖子缩略图?

时间:2015-09-24 作者:Nasim Firoz

我使用下面的代码。但代码在“五位数”列中显示缩略图。如何将其移动到第一列位置?

//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;
}
}

2 个回复
最合适的回答,由SO网友:Nick 整理而成

您可以使用此代码在post列中添加特色图像缩略图。

从此处复制Add Featured Post Thumbnails to WordPress Admin Post Columns.我自己还没试过,但一定有用。

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

刚试过,效果很好。

虽然它可以很好地开箱即用,但如果您想使用自定义的图像大小,可以在上面的代码中更改缩略图大小。

EDIT

刚刚修复了80x80缩略图的代码。

SO网友:Husky

下面是由发布的代码的一个稍微干净的版本Nick. 有几个区别:

我使用匿名函数来避免全局名称空间混乱。请注意,如果只需要将代码添加到单个post类型,还可以使用闭包:

add_filter(\'manage_posts_columns\', function() {
    // Do your thing here
});
<而不是返回全新的$columns 数组,我正在现有数组中添加列。如果添加了另一列(通过WordPress更新或另一段代码),此方法不会破坏这一点if 在manage_posts_custom_column 钩子而不是switch 在这里似乎更有意义
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 );