【问题标题】:How to use wordpress update_option() function to modify medium_large image deafault's width如何使用 wordpress update_option() 函数修改 medium_large 图片默认宽度
【发布时间】:2017-06-07 10:28:43
【问题描述】:

在最近的 wordpress 版本中,添加了一个新的默认中间尺寸“medium_large”,以更好地利用响应式图像支持。新尺寸默认为 768px 宽,没有高度限制。

在选择要在帖子中插入的图像时,UI 中不包含 medium_large 尺寸,也不包含用于从媒体设置页面更改图像大小的 UI。但是,开发人员可以使用 update_option() 函数修改此新尺寸的宽度,类似于任何其他默认图像尺寸。

用法:

<?php update_option( $option, $new_value, $autoload ); ?>

考虑到我已经尝试通过functions.php文件中的以下代码编辑默认的medium_large图像大小但没有成功,请您帮我通过update_option构建代码,如上所述?

function filter_image_resize_dimensions( $payload, $orig_w, $orig_h, $dest_w, $dest_h, $crop ) {
if ( $orig_h > 200 ) {
    $new_w = 200;
    $new_h = auto;
}

return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h );

} add_filter('image_resize_dimensions', 'filter_image_resize_dimensions', 10, 6);

【问题讨论】:

    标签: php html wordpress


    【解决方案1】:

    过滤器max_srcset_image_width不是用来改变图片大小的最大宽度,而是使用wp_calculate_image_srcset()时srcset的最大宽度。

    如果要更改 WordPress 预定义图像的大小,您需要使用image_resize_dimensions,您可以阅读所有详细信息here

    更新 根据您给出的代码,这里有一些更正

    function filter_image_resize_dimensions( $payload, $orig_w, $orig_h, $dest_w, $dest_h, $crop ) {
        if ( $orig_h === 300 ) {
            $new_w = 200; // you are checking for height and setting width ??
            $new_h = $orig_h; // you must define it to return a value, but here there's no change... 
        }
    
        return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h );
    }
    add_filter( 'image_resize_dimensions', 'filter_image_resize_dimensions', 10, 6 );
    

    希望对你有帮助

    【讨论】:

    • 我重试了这种方式,但又失败了: function filter_image_resize_dimensions( $max_width, $size_array ) { if ( $size_array[0] === 300 ) { $max_width = 200; } 返回 $max_width; } add_filter('image_resize_dimensions', 'filter_image_resize_dimensions', 10, 2);
    • 回调函数的参数和你之前的函数不一样,看我的链接提供的例子,没有$size_array,但是$orig_h,$orig_w。更改条件语句并将过滤器最大参数更改为 6,它应该可以工作。
    • if ( $orig_h > 200 ) { $new_w = 200; $new_h = 自动; } --- 这段代码正确吗?
    • (我想将宽度调整为 200,高度自动,但根据调整后的宽度按比例增加)
    • 这取决于您的主题(或插件),当您上传图像时,WordPress 会根据声明的 image_size 保存不同的大小,使用 add_image_size() 函数触发,您可以轻松删除任何 image_size使用函数remove_image_size(但你需要找到它的slug才能正确删除它),你也可以通过filter修改add_image_size参数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    • 2021-06-16
    • 1970-01-01
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多