文件上传
调用wordpress默认的媒体中心,上传文件,并将文件地址填写到input输入框中实现,可预览图片。
范例:
- $ashu_meta[] = array(
- 'name' => 'File Upload Example',
- 'id' => 'upload_example',
- 'desc' => 'Pleas upload a image, Or fill the blank with image url',
- 'std' => '',
- 'button_text' => '上传',
- 'type' => 'upload',
- 'multiple' => 'false'
- );
范例2:
- $ashu_meta[] = array(
- 'name' => 'Gallery Example',
- 'id' => 'gallery_example',
- 'desc' => 'Pleas upload some images.',
- 'std' => '',
- 'button_text' => 'Add images',
- 'type' => 'gallery',
- 'multiple' => 'false'
- );
详解:
name - 自定义字段标题
id - 自定义字段名称,获取数据时用。敬告:请确配置文件中所有id都不相同,同时避免post\page\title等词。
desc - 自定义字段描述信息
std - 默认值
button_text - 上传按钮的文字,默认为Upload
type - 值为upload时,本条字段类型为文件上传;值为gallery时可一次上传多个图片。
multiple - 输入框自增,若值为true,则文件上传输入框的个数可点击增加。
多图片调用范例
'type'=>'gallery'时,在数据库中以数组形式保存一组图片的ID,所以调用的时候需要循环数组,根据ID再获取图片的地址:
- <?php
- $attachment_ids = get_post_meta($post_id,'gallery_example',true);
- if( !empty($attachment_ids) && is_array($attachment_ids) ){
- foreach($attachment_ids as $attachment_id){
-
- $attachment_src = wp_get_attachment_image_src( $attachment_id, 'full' );
- if ( $attachment_src ){
- echo '<img src="'.$attachment_src[0].'" />';
- }
- }
- }