是否添加自定义图像上传大小并将其设为默认选中?

时间:2012-09-23 作者:Amanda Bynes

如何添加宽度为550px的自定义图像上载大小,并在添加媒体页面中默认选中?

目前,有4种尺寸可供选择:Thumbnail, Medium, LargeFull Size. 这个Full Size 默认情况下选择选项。

有人知道解决这个问题的有效方法吗?

2 个回复
最合适的回答,由SO网友:Paul d\'Aoust 整理而成

There are a few steps to this, as I found out today. First, you add a custom image size (you can put this somewhere the functions.php file in your theme):

add_theme_support( \'post-thumbnails\' );
add_image_size( \'my-size\', 550, 550 ); // not sure what you want your height to
                                     // be, but this\'ll shrink it to 550px
                                     // wide *or* tall. If you want it to be
                                     // cropped to a 550px square, add true as
                                     // a fourth argument

The unfortunate thing is that WordPress doesn\'t actually show this new size in the media uploader. You\'ll have to put a function that does this into your functions.php as well (source: http://kucrut.org/insert-image-with-custom-size-into-post/):

function my_insert_custom_image_sizes( $sizes ) {
    // get the custom image sizes
    global $_wp_additional_image_sizes;
    // if there are none, just return the built-in sizes
    if ( empty( $_wp_additional_image_sizes ) )
        return $sizes;

    // add all the custom sizes to the built-in sizes
    foreach ( $_wp_additional_image_sizes as $id => $data ) {
        // take the size ID (e.g., \'my-name\'), replace hyphens with spaces,
        // and capitalise the first letter of each word
        if ( !isset($sizes[$id]) )
            $sizes[$id] = ucfirst( str_replace( \'-\', \' \', $id ) );
    }

    return $sizes;
}

// apply the above function as a filter on the media uploader\'s list of
// image sizes
add_filter( \'image_size_names_choose\', \'my_insert_custom_image_sizes\' );

Lastly, to set this size as the default, you\'ll need to change a WordPress setting. Add a filter on the setting in question. This has the benefit of being portable with your theme in which you\'ve defined all the above options.

function my_set_default_image_size () {
    return \'my-size\';
}
add_filter( \'pre_option_image_default_size\', \'my_set_default_image_size\' );

I tested this all out on WordPress 3.4.2, and it seems to work great.

Note: I\'d recommend putting the above PHP code into an init function of some sort and attaching it to the \'after_setup_theme\' hook:

// define the functions first
function my_insert_custom_image_sizes( $sizes ) {
    // not going to repeat the function body, for brevity\'s sake
}

function my_set_default_image_size() {
    // ditto
}

// define the init function next, which sets up all the necessary stuff
function custom_image_setup () {
    add_theme_support( \'post-thumbnails\' );
    add_image_size( \'my-size\', 550, 550 );
    add_filter( \'image_size_names_choose\', \'my_insert_custom_image_sizes\' );
    add_filter( \'pre_option_image_default_size\', \'my_set_default_image_size\' );
}

// and attach that init function to the \'after_setup_theme\' hook
// so it runs on each page load once your theme\'s been loaded
add_action( \'after_setup_theme\', \'custom_image_setup\' );
SO网友:kaiser

只需将所有其他选项设置为0 在“设置”»“媒体”下的管理面板中。这将禁用它们。

结束

相关推荐

强制将Single.php重定向到索引

我想对single进行强制重定向。php索引,这样就永远无法通过直接访问访问它(网站的构建方式是检索索引页面上的所有内容,但由于ajax功能,仍然需要此页面可用)。我想这是在。htaccess?编辑:我需要能够访问单曲。php通过来自索引页的ajax请求进行重定向,也就是说,重定向应该只在直接访问url时发生。