2011-02-17 20 views
2

我知道如何在PHP中使用add_image_size来注册一个可以在模板中使用的新大小。 当它们上传时,它也会以此大小制作图像。如何将自定义图片大小添加到WordPress,但让他们在管理员中?

虽然我添加的尺寸不会显示在管理员中。

所以2个问题...

  • 我怎样才能获得的图像尺寸,我让出现在“上传图片”的事情时,博客进行新的帖子?

  • 有没有办法在管理员中列出它,以便博主可以添加新的,而无需更改PHP代码?

还是我做错了什么,如不把add_image_size呼叫在正确的地方/文件?这样的大小应该显示在上传菜单部分?

科尔

回答

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] . '&nbsp;×&nbsp;' . $downsize[2] . ')</label>'; 
    $form_fields['image-size']['html'] .= '<div class="image-size-item">' . $html . '</div>'; 
    } 

    return $form_fields; 
} 
+0

很酷,谢谢! – Cole 2011-02-20 05:18:49

相关问题