文件上传

调用wordpress默认的媒体中心,上传文件,并将文件地址填写到input输入框中实现,可预览图片。

范例:

  1. $ashu_meta[] = array(
  2.   'name' => 'File Upload Example',
  3.   'id'   => 'upload_example',
  4.   'desc' => 'Pleas upload a image, Or fill the blank with image url',
  5.   'std'  => '',
  6.   'button_text' => '上传',
  7.   'type' => 'upload',
  8.   'multiple' => 'false'
  9. );

范例2:

  1. $ashu_meta[] = array(
  2.   'name' => 'Gallery Example',
  3.   'id'   => 'gallery_example',
  4.   'desc' => 'Pleas upload some images.',
  5.   'std'  => '',
  6.   'button_text' => 'Add images',
  7.   'type' => 'gallery',
  8.   'multiple' => 'false'
  9. );

详解:

name - 自定义字段标题

id - 自定义字段名称,获取数据时用。敬告:请确配置文件中所有id都不相同,同时避免post\page\title等词。

desc - 自定义字段描述信息

std - 默认值

button_text - 上传按钮的文字,默认为Upload

type - 值为upload时,本条字段类型为文件上传;值为gallery时可一次上传多个图片。

multiple - 输入框自增,若值为true,则文件上传输入框的个数可点击增加。

多图片调用范例

'type'=>'gallery'时,在数据库中以数组形式保存一组图片的ID,所以调用的时候需要循环数组,根据ID再获取图片的地址:

  1. <?php
  2. //获取字段的值,注意$post_id为文章ID,'gallery_example'为字段配置ID,请根据实际情况更改
  3. $attachment_ids = get_post_meta($post_id,'gallery_example',true);
  4. if( !empty($attachment_ids) && is_array($attachment_ids) ){
  5. foreach($attachment_ids as $attachment_id){ //循环图片ID
  6.   //获取图片地址,请参考wp的wp_get_attachment_image_src函数
  7.   $attachment_src = wp_get_attachment_image_src( $attachment_id, 'full' );
  8.   if ( $attachment_src ){
  9.     echo '<img src="'.$attachment_src[0].'" />';
  10.   }
  11. }
  12. }