在CPT编辑屏幕中调整管理员特色图像缩略图的大小

时间:2014-09-10 作者:SolaceBeforeDawn

我有一个滑块的自定义帖子类型,我正在使用特色图像缩略图上传器来设置图像。

我已经重新定位了特色图片元框,使其位于管理中标题部分的下方。现在,为了避免用户混淆,我希望以上传图像的比例尺寸(在本例中为800px宽300px高的滑块图像)显示特色图像“缩略图”(在管理CPT编辑器屏幕中)。

目前图像显示为266px宽,我相信是这样this function that is controlling it.

我确实意识到,修改它只是为了苦行僧,它不会影响前端图像的“输出”大小(我知道如何控制它)。但是,上载滑块图像的用户不希望看到缩略图。

这只是为了以正确的比例向用户显示他们上传的图像。

目前我正在使用它来移动特色图像元框;

// move the featured image box and rename to 
add_action(\'do_meta_boxes\', \'wpse33063_move_meta_box\');

function wpse33063_move_meta_box(){
remove_meta_box( \'postimagediv\', \'slider-image\', \'side\' );
add_meta_box(\'postimagediv\', __(\'Add Your Slider Image\'), \'post_thumbnail_meta_box\', \'slider-image\', \'normal\', \'high\');
}
我见过this post, this onethis one (更多与TwentyTen相关),其他人建议在管理员自定义后期编辑屏幕中显示重新调整大小的“缩略图”的方法。我尝试过这些方法,但似乎没有一种有效/都返回服务器错误。

Update: I answered my own question after finding a method in WordPress Trac - see below

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

好吧,我在这里找到了答案trac ticket, 所以我要向谢尔盖·比柳科夫致敬。我在这里为任何想知道的人发帖;-)然而,我还没有破解定制尺寸(例如,800px宽200px高),这将是一件好事。

// move the featured image box from the right hand column, to sit under the title section, and rename it 
add_action(\'do_meta_boxes\', \'wpse33063_move_meta_box\');

function wpse33063_move_meta_box(){
remove_meta_box( \'postimagediv\', \'slider-image\', \'side\' );
add_meta_box(\'postimagediv\', __(\'Add Your Slider Image\'), \'post_thumbnail_meta_box\', \'slider-image\', \'normal\', \'high\');
}



// change size of admin featured image size in edit screen 
function change_featured_image_size_in_admin_28512( $downsize, $id, $size ) {
if ( ! is_admin() || ! get_current_screen() || \'edit\' !== get_current_screen()->parent_base ) {
    return $downsize;
}

remove_filter( \'image_downsize\', __FUNCTION__, 10, 3 );

// settings can be thumbnail, medium, large, full 
$image = image_downsize( $id, \'medium\' ); 
add_filter( \'image_downsize\', __FUNCTION__, 10, 3 );

return $image;
}
add_filter( \'image_downsize\', \'change_featured_image_size_in_admin_28512\', 10, 3 );

SO网友:rambillo

要实现自定义大小(更新/保存帖子后),请尝试以下操作。。。

// Step 1   - define our custom image size so we can call it instead of the system defined default \'post-thumbnail\' 
add_image_size( \'slider_image\', 800, 300, true );

// Step 2   - replace the meta box with ours for our CPT \'slider-image\' only, especially so we can set our own callback. The rename is nice, too.
add_action(\'do_meta_boxes\', \'wpse33063_move_meta_box\');

function wpse33063_move_meta_box(){
    remove_meta_box( \'postimagediv\', \'slider-image\', \'side\' );
    add_meta_box(\'postimagediv\', __(\'Add Your Slider Image\'), \'wpse33063_post_thumbnail_meta_box\', \'slider-image\', \'normal\', \'high\');
}

// Step 3   - custom callback to call our functional replacement for _wp_post_thumbnail_html for our CPT \'slider-image\' only
function wpse33063_post_thumbnail_meta_box( $post ) {
    $thumbnail_id = get_post_meta( $post->ID, \'_thumbnail_id\', true );
    echo wpse33063_wp_post_thumbnail_html( $thumbnail_id, $post->ID );
}

// Step 4   - replace _wp_post_thumbnail_html with our version that calls our thumbnail \'slider_image\' instead of the default \'post-thumbnail\'
//          - we could do more here, like adjust the content width variable, but not entirely necessary. The custom image size we defined will
//          - handle most of it, plus the admin section these days has responsive css, so the image doesn\'t blow-out column width in any event.
function wpse33063_wp_post_thumbnail_html( $thumbnail_id = null, $post = null ) {
global $content_width, $_wp_additional_image_sizes;

$post               = get_post( $post );
$post_type_object   = get_post_type_object( $post->post_type );
$set_thumbnail_link = \'<p class="hide-if-no-js"><a title="%s" href="%s" id="set-post-thumbnail" class="thickbox">%s</a></p>\';
$upload_iframe_src  = get_upload_iframe_src( \'image\', $post->ID );

$content = sprintf( $set_thumbnail_link,
    esc_attr( $post_type_object->labels->set_featured_image ),
    esc_url( $upload_iframe_src ),
    esc_html( $post_type_object->labels->set_featured_image )
);

if ( $thumbnail_id && get_post( $thumbnail_id ) ) {
    $old_content_width = $content_width;
    $content_width = 266;
    if ( !isset( $_wp_additional_image_sizes[\'slider_image\'] ) )    // use our custom image size instead of \'post-thumbnail\'
        $thumbnail_html = wp_get_attachment_image( $thumbnail_id, array( $content_width, $content_width ) );
    else
        $thumbnail_html = wp_get_attachment_image( $thumbnail_id, \'slider_image\' ); // use our custom image size instead of \'post-thumbnail\'
    if ( !empty( $thumbnail_html ) ) {
        $ajax_nonce = wp_create_nonce( \'set_post_thumbnail-\' . $post->ID );
        $content = sprintf( $set_thumbnail_link,
            esc_attr( $post_type_object->labels->set_featured_image ),
            esc_url( $upload_iframe_src ),
            $thumbnail_html
        );
        $content .= \'<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail" onclick="WPRemoveThumbnail(\\\'\' . $ajax_nonce . \'\\\');return false;">\' . esc_html( $post_type_object->labels->remove_featured_image ) . \'</a></p>\';
    }
    $content_width = $old_content_width;
}

/**
 * Filter the admin post thumbnail HTML markup to return.
 *
 * @since 2.9.0
 *
 * @param string $content Admin post thumbnail HTML markup.
 * @param int    $post_id Post ID.
 */
return apply_filters( \'admin_post_thumbnail_html\', $content, $post->ID );
}

SO网友:rjb

开始于WordPress 4.4, 引入了一种新的过滤器,admin_post_thumbnail_size, 它允许修改特征图像元框中后期缩略图图像的显示大小。

当主题添加“后期缩略图”支持时,会注册一个特殊的“后期缩略图”图像大小,它不同于通过设置>媒体屏幕管理的“缩略图”图像大小。

我们可以使用此筛选器更改post-thumbnail 大小(266 x 266 px),并将其修改为使用默认值thumbnail 尺寸(150 x 15像素)。

/**
 * Change Display Size of Featured Image Thumbnail in WordPress Admin Dashboard
 */
add_filter( \'admin_post_thumbnail_size\', function ( $size, $thumbnail_id, $post ) {
    $sizes = get_intermediate_image_sizes();

    $result = array_search( \'thumbnail\', $sizes );

    $size = is_numeric( $result ) ? $sizes[ $result ] : array( 100, 100 );

    return $size;
}, 10, 3 );
还可以指定或设置特定大小(例如100 x 100 px)作为备用大小,以防intermediate thumbnail size 不可用。

结束