如何在Media Upload中默认选择图像大小-WP v3.5

时间:2012-12-15 作者:Christine Cooper

与我赤裸。我想在媒体上载弹出页面中默认选择自定义图像大小。在Wordpress v3中。4.2及之前,此优雅的代码运行良好:

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


// Which custom image size selected by default
function my_set_default_image_size () { 
     return \'custom-image-size-2\';
}


function custom_image_setup () {
    add_theme_support( \'post-thumbnails\' );
    add_image_size( \'custom-image-size-1\', 160, 9999 ); //  columned
    add_image_size( \'custom-image-size-2\', 300, 9999 ); //  medium
    add_image_size( \'custom-image-size-3\', 578, 190, true ); //  cropped
    add_filter( \'image_size_names_choose\', \'my_insert_custom_image_sizes\' );
    add_filter( \'pre_option_image_default_size\', \'my_set_default_image_size\' );
}

add_action( \'after_setup_theme\', \'custom_image_setup\' );
所以,my_insert_custom_image_sizes 将自定义图像添加到媒体页面,然后my_set_default_image_size 应选择custom-image-size-2 大小此代码已停止使用Wordpress 3.5版本。您知道我如何在v3中实现这一点吗。5.

2 个回复
SO网友:Eric Holmes

试试这个。你的add_filter()\'s 第二个参数是一个函数,它将通过返回影响当前选项:

function theme_default_image_size() {
    return \'custom-image-size-2\';
}
add_filter( \'pre_option_image_default_size\', \'theme_default_image_size\' );
您还可以查看pre_update_option_{$option} 筛选并更新该值一次,因此无需每次都运行此筛选器(可能会节省0.01秒,但仍在保存!):)

还是老古董update_option():

update_option( \'image_default_size\', \'custom-image-size-2\' );

SO网友:Selva Balaji

将函数添加到主题的函数中。php文件。

if ( function_exists( \'add_theme_support\' ) ) {
    add_theme_support( \'post-thumbnails\' );
        set_post_thumbnail_size( 150, 150 ); // default Post Thumbnail dimensions   
}


function custom_image_setup () {

        add_theme_support(\'post-thumbnails\');
        set_post_thumbnail_size(640,320);

    add_image_size( \'custom-image-size-1\', 180, 9999 ); //  columned
    add_image_size( \'custom-image-size-2\', 350, 9999 ); //  medium
    add_image_size( \'custom-image-size-3\', 600, 250, true ); //  cropped

    add_filter( \'image_size_names_choose\', \'theme_custom_image_sizes\' );
    add_filter( \'pre_option_image_default_size\', \'theme_default_image_size\' );
}


if ( function_exists( \'add_image_size\' ) ) { 
    add_image_size( \'category-thumb\', 300, 9999 ); //300 pixels wide (and unlimited height)
    add_image_size( \'homepage-thumb\', 220, 180, true ); //(cropped)
}
在主题的模板文件中使用新的图像大小。

if ( has_post_thumbnail() ) { the_post_thumbnail( \'category-thumb\' ); }

结束

相关推荐

Wassup中显示的ODD/wp-admin/admin-ajax.php条目

我有一个奇怪的问题,我不知道如何解决。自从我安装了一个流行的帖子插件(Wordpress popular Posts)以来,我一直在Wassup实时分析中看到一个奇怪的条目,该页面名为/wp admin/admin ajax。php一次又一次。在我安装插件之前,它会显示被点击的页面,但无论出于何种原因,该页面看起来都会被点击,我无法看到用户实际点击的页面。下面是我所说的一个例子:编辑:How do I get the correct page/post to be displayed instead of