在wordpress中创建自定义字段并上传图片
问题描述:
我正在尝试学习通过自定义字段上传图像文件的方法,但无法获得最简单的代码.我只是在这里做了一点:
i'm trying to learn the way for uploading image file through custom field but cant get the easiest code to do it. i just done a bit here:
add_action('admin_init', 'create_image_box');
function create_image_box() {
add_meta_box( 'meta-box-id', 'Image Field', 'display_image_box', 'post', 'normal', 'high' );
}
//Display the image_box
function display_image_box() {
global $post;
$image_id = get_post_meta($post->ID,'xxxx_image', true);
echo 'Upload an image: <input type="file" name="xxxx_image" id="xxxx_image" />';
// Upload done: show it now...(as thmbnail or 60 x 50)
任何人都请带我到下一步,并展示在博客页面中显示图像的方式.
anybody please take me to next step and show the way to display the image in blog page too.
答
让我们循序渐进:
- 创建自定义字段元框,用于在帖子类型 => 帖子中插入图片网址.
- 在后端更新/保存自定义字段值.
- 在前端显示自定义字段值.
看到你的代码,你似乎错过了#2.试试下面的代码来保存自定义字段:
Seeing your code it seems that you are missing #2. Try the code below to save custom field:
function save_joe_details($post_id){
global $post;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
update_post_meta($post->ID, "custom_field_image", $_POST["custom_field_image"] );
}
add_action('save_post', 'save_joe_details');
显示自定义字段的 #3 代码为:
Code for #3 that displaying the custom field will be:
<?php global $post;
$custom_image = get_post_custom($post->ID); ?>
<img src="<?php echo $custom_image["custom_field_image"][0] ?>" />