我知道如何在PHP中使用add_image_size来注册可以在模板中使用的新大小。 它在上传时也会以这种尺寸制作图像。
我添加的大小不会在管理员中显示。
所以2个问题......
当博主发布新帖时,如何让我在“上传图片”中显示的图片尺寸显示?
有没有办法在管理员中列出它,以便博主可以添加新的,而无需更改PHP代码?
或者我做错了什么,比如不把add_image_size调用放在正确的位置/文件中?这样的尺寸应该出现在上传菜单部分吗?
科尔
答案 0 :(得分:8)
Wordpress不会在媒体灯箱中显示自定义图像尺寸选项,但您可以使用attachment_fields_to_edit
过滤器添加它们。以下内容将为您定义的所有自定义图像尺寸添加选项。
add_filter('attachment_fields_to_edit', 'my_attachment_fields_to_edit_filter', 100, 2);
function my_attachment_fields_to_edit_filter($form_fields, $post) {
if (!array_key_exists('image-size', $form_fields)) return $form_fields;
global $_wp_additional_image_sizes;
foreach($_wp_additional_image_sizes as $size => $properties) {
if ($size == 'post-thumbnail') continue;
$label = ucwords(str_replace('-', ' ', $size));
$cssID = "image-size-{$size}-{$post->ID}";
$downsize = image_downsize($post->ID, $size);
$enabled = $downsize[3];
$html = '<input type="radio" ' . disabled($enabled, false, false) . 'name="attachments[' . $post->ID. '][image-size]" id="' . $cssID . '" value="' . $size .'">';
$html .= '<label for="'. $cssID . '">' . $label . '</label>';
if ($enabled) $html .= ' <label for="' . $cssID . '" class="help">(' . $downsize[1] . ' × ' . $downsize[2] . ')</label>';
$form_fields['image-size']['html'] .= '<div class="image-size-item">' . $html . '</div>';
}
return $form_fields;
}