获取上传图片的详细信息

时间:2012-06-06 作者:Eray

我在用Wordpress的上传系统(as explained here) 在插件的管理页面上。

我可以上传一张图片并获取该图片的URL。但我需要附件ID、图像标题等。我如何才能访问它们?

我有一个<form> . 它有3个<input>s

<input type="text" value="" /> //This input will be filled, after user upload an image and click to `Insert to post` button. I can do it.
<input type="button" value="Click for upload image" />
<input type="hidden" value="" name="ID" /> //Attachment ID will come here automagically (with JavaScript, maybe?). I don\'t know how can i get it.

3 个回复
SO网友:Chris_O

为图像存储的附件元数据为:

Array
(
    [width] => 558
    [height] => 771 
    [hwstring_small] => height=\'62\' width=\'128\'
    [file] => 2012/02/logo2.png
    [sizes] => Array
    (
        [thumbnail] => Array
        (
            [file] => file_name.png
            [width] => 150
            [height] => 150
        )

        [medium] => Array
        (
            [file] => logo2-300x145.png
            [width] => 300
            [height] => 145
        )
    )
    [image_meta] => Array
    (
        [aperture] => 0
        [credit] => 
        [camera] => 
        [caption] => 
        [created_timestamp] => 0
        [copyright] => 
        [focal_length] => 0
        [iso] => 0
        [shutter_speed] => 0
        [title] => 
    )
)
如果图像已附加到帖子,则可以使用get\\u children():

$id = intval( $post->ID );  //The ID of the post attached to.
$attachments = get_children( array(
            \'post_parent\' => $id,
            \'post_status\' => \'inherit\',
            \'post_type\' => \'attachment\',
            \'post_mime_type\' => \'image\',
            \'order\' => \'ASC\',
            \'orderby\' => \'menu_order\'
        ) );

foreach ( $attachment as $id => $attachment ) {
      $caption = $attachment[\'image_meta\']->caption;
    }

SO网友:fdsa

如果您正在复制文章,则这些字段不存在。添加一些表单字段并获取这些值可能是最简单的。

如果您假设这些图像已经上传到WordPress并存储在wp内容中,您可以在wp帖子的帖子类型附件下找到wp数据库中的图像(source).

但是,从文章的设置方式来看,似乎可以从用户的计算机或其他网站上传图像,因此最好让他们填写字段。

SO网友:Tommixoft

给你。如果我理解正确,您希望在上传附件后获取信息。

所以把这个代码放进去functions.php 文件这里有两个功能-一个是在上传图像并显示以编辑信息后启动的(或总是在编辑附件时启动),另一个是在用户单击附件上的保存时启动的。

使用这两个函数,您可以制作任何您想要的内容:)

add_filter("attachment_fields_to_edit", "wpse_54409_image_details_on_edit", null, 2); 
add_filter("attachment_fields_to_save", "wpse_54409_image_details_on_save", null , 2);

function wpse_54409_image_details_on_edit($form_fields, $post) {
     //we interested only in images so ->
     if( substr($post->post_mime_type, 0, 5) == \'image\' ){
         $meta = get_post_custom($post->ID);
          echo \'This attachment ID: \'.$post->ID;
          echo \'<br>\';
          echo \'This attachment title: \'.$post->post_title;
          echo \'<br>\';
          echo \'This attachment post name: \'.$post->post_name; 
          echo \'<br>\'; 
          echo \'Alt. text: \'.$meta[\'_wp_attachment_image_alt\'][0]; 
          echo \'<br>\'; 
          echo \'File name: \'.$meta[\'_wp_attached_file\'][0]; 
     }
     return $form_fields;
 }

function wpse_54409_image_details_on_save($post, $attachment) {
    //we interested only in images so ->
    if( substr($post->post_mime_type, 0, 5) == \'image\' ){
        /*
        HERE YOU CAN GET ALL INFO UPPON ATTACHMENT IS SAVED 
        AND DO WITH THAT INFO WHATEVER YOU WANT
        EVEN ALTER IT TO MAKE CHANGES TO IT
        */
    }
    return $post;
}

结束

相关推荐

404从wp-Content/Uploads/获取图像时

我在获取图像时获得404状态,http仍然包含该图像。图像显示在浏览器中,但404代码中断了一些应用程序。对wp内容/上载/的调用被重定向到。htaccess:<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\\.php$ - [L] RewriteRule (.*) /index.php?getfile=$1 [L] </IfModule>&