管理面板中的特色图像大小?

时间:2013-06-28 作者:andy

我想完全清楚这一点,我想做的是改变管理面板上的特色图像缩略图的大小,而不是在前端。

我正在建立一个可湿性粉剂管理主题,我需要使后端的特色形象更大。

看起来,编辑贴子屏幕中的特色图像总是以266x148px的速度显示,理想情况下,我希望它们是实际大小。我到处搜索,找不到任何可以让我这样做的挂钩或函数,但如果有人知道怎么做,那就是这里的人。

3 个回复
SO网友:fuxia

您可以筛选\'admin_post_thumbnail_html\' 并更换img 具有所需大小的新元素。

其他一切都会变得混乱,并可能导致副作用。

过滤image_downsize 同时观察266×266 可能会影响不打算显示在metabox中的图像post-thumbnail 临时并不容易,因为没有安全点来启动和结束这样的过滤器

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 不可用。

SO网友:rambillo

You could try this

/**
 * step 1   define custom image size
 *
 *          either plan to use \'full\' (returns the size of the image you uploaded)
 *          OR define our custom image size so we can call one of those instead
 *          of the system defined default \'post-thumbnail\'
 *
 *          Remainder of this example assumes using \'full\'
 *          no need to define custom a custom image size
 *          so remove the following add_image_size() line (and thereby skip step 1).
 */
//add_image_size( \'admin-post-thumbnail\', 846, 288, true );

/**
 * step 2   replace the meta box with ours, for standard post type only
 *
 *          set our own callback and move it into the larger main column
 *
 *          Note the image, even in full size, won\'t ever have greater width than
 *          the current column width as CSS and modern admin panel make the image
 *          elements responsive to the column width.
 */
 add_action(\'do_meta_boxes\', \'wpse104670_move_meta_box\');

function wpse104670_move_meta_box(){
    remove_meta_box( \'postimagediv\', \'post\', \'side\' );
    add_meta_box(\'postimagediv\', __(\'Featured Image\'), \'wpse104670_post_thumbnail_meta_box\', \'post\', \'normal\', \'low\');
}

// step 3   custom callback to call our functional replacement for _wp_post_thumbnail_html for post type only
function wpse104670_post_thumbnail_meta_box( $post ) {
    $thumbnail_id = get_post_meta( $post->ID, \'_thumbnail_id\', true );
    echo wpse104670_wp_post_thumbnail_html( $thumbnail_id, $post->ID );
}

/**
 * step 4   replace _wp_post_thumbnail_html with our version that calls \'full\'
 *          (or e.g. \'admin-post-thumbnail\' if custom)
 *          instead of the default \'post-thumbnail\'
 *
 *          We could do more here, like adjust the content width variable, but
 *          not entirely necessary as custom image size we defined will
 *          handle most of it and admin section has responsive CSS keeping image
 *          from ever exceeding column width.
 */
function wpse104670_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[\'full\'] ) )    // use \'full\' for system defined fullsize image OR 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, \'full\' ); // use \'full\' for system defined fullsize image OR 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 );
}
结束

相关推荐