【问题标题】:create custom field and upload image in wordpress在 wordpress 中创建自定义字段并上传图片
【发布时间】:2012-11-26 10:04:32
【问题描述】:

我正在尝试学习通过自定义字段上传图像文件的方式,但无法获得最简单的代码来做到这一点。我刚刚在这里做了一点:

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) 

任何人都请带我进入下一步,并展示在博客页面中显示图像的方式。

【问题讨论】:

    标签: wordpress image-upload


    【解决方案1】:

    让我们逐步进行:

    1. 创建自定义字段元框以在帖子类型中插入图片网址 => 帖子。
    2. 在后端更新/保存自定义字段值。
    3. 在前端显示自定义字段值。

    看到您的代码,您似乎缺少#2。试试下面的代码来保存自定义字段:

    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 显示自定义字段的代码将是:

    <?php global $post;
    $custom_image = get_post_custom($post->ID); ?>
    <img src="<?php echo $custom_image["custom_field_image"][0] ?>" />
    

    【讨论】:

    • 感谢乔的帮助。我只是按照你的方式做,没有出错,但我做错了,那就是它没有显示图像。。[stackoverflow.com/questions/13795277/…
    • 尝试确认自定义字段在帖子编辑器中是否包含任何值。 (如果 自定义字段小部件 被隐藏,则使用屏幕顶部的屏幕选项使其可见。)
    • 感谢 4ur 回复..我刚刚检查了屏幕选项,所有标记为可见。在我的代码中做错了什么? [stackoverflow.com/questions/13795277/…
    • 您是否检查了自定义字段值,是否包含任何图像 url?
    • 它只包含文件名而不是url..我怎样才能得到url?
    【解决方案2】:

    您可以尝试将其包装在 wp_get_attachment_url 中,如下所示:-

    wp_get_attachment_url( $custom_image["custom_field_image"][0] ); 
    

    【讨论】:

      猜你喜欢
      • 2012-01-18
      • 1970-01-01
      • 1970-01-01
      • 2018-08-12
      • 1970-01-01
      • 2012-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多