【问题标题】:How can I add custom image sizes to wordpress but have them in the admin?如何将自定义图像大小添加到 wordpress 但将它们放在管理员中?
【发布时间】:2011-06-29 07:23:48
【问题描述】:

我知道如何在 PHP 中使用 add_image_size 来注册一个可以在模板中使用的新尺寸。 上传时,它也会制作这种尺寸的图像。

不过,我添加的尺寸不会显示在管理中。

那么 2 个问题...

  • 当博主发布新帖子时,如何让我制作的图片尺寸显示在“上传图片”中?

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

或者我做错了什么,比如没有将 add_image_size 调用放在正确的位置/文件中?这样的尺寸是否应该显示在上传菜单部分?

科尔

【问题讨论】:

    标签: php image wordpress


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2011-03-20
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 2019-05-19
      • 1970-01-01
      • 2013-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多