如何在媒体上传-WP v3.5中默认选择图像尺寸


12

和我一起裸露。我想在“媒体上载”弹出页面中默认选择一个自定义图像尺寸。在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中做到这一点吗?


这并不直接回答你的问题,但也是种相关:stackoverflow.com/questions/13936080/...
janw

Answers:


2

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

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.01s,但仍在保存!):)

或旧的update_option()好

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

0

将该功能添加到主题的functions.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' ); }
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.