Image display from custom field

I am trying to display image from my custom meta box but cant make it:

function.php file:

add_action( 'admin_init', 'order_box' );
function order_box() {
add_meta_box( 'meta-box-id', 'My Custom Field', 'my_custom_field', 'post', 'normal', 'high' );
}
function my_custom_field(){ 
global $post;
$custom = get_post_custom($post->ID);
$order=$custom["order"][0];
$image=$custom["image"][0];
?>
<label for="custom_field">Put Order:</label>
<input type="text" name="order" value="<?php echo $order; ?>" />
<label for="custom_field">Upload Image:</label>
<input type="file" name="image" />
<?php  
} 

 add_action('save_post', 'save_order');
 function save_order(){
 global $post;
 update_post_meta($post->ID, "order", $_POST["order"]);
 update_post_meta($post->ID, "image", $_POST["image"] );
 }

my template file where i want display the image:

<?php
while (have_posts()) : the_post(); ?>
<h1><a href="https://wordpress.stackexchange.com/questions/75550/<?php the_permalink();?>">  <?php the_title(); ?></a> </h1>
<?php the_content(); ?>
<?php 
$custom = get_post_custom($post->ID);
$image=$custom["image"][0];
$order=$custom["order"][0];
echo $order;
?>
//***  <img src="<?php echo $image; ?>" /> 
<img src="<?php echo get_post_meta($post->ID,'image',true); ?>" 
    width=200 height=200 />
 endwhile;

here ‘order’ is displayed nicely but not image. print_r($custom) got something like this:

Array ( 
    [_edit_last] => Array ( [0] => 1 ) 
    [order] => Array ( [0] => 13 ) 
    [image] => Array ( [0] => coffee_star.jpg ) 
)

someone please help me to display the image in my template page. I am very new to wordpress. Thanks in advance.

1 Answer
1

If you use update_post_meta() than also use get_post_meta() (see Codex) and not get_post_custom

$order = get_post_meta( $post->ID, 'order', true );
$image = get_post_meta( $post->ID, 'image', true );

And than check in your HTML source what your image tag looks like. I bet it will look like this:

<img src="https://wordpress.stackexchange.com/questions/75550/coffee_star.jpg" />

What about the path to the image?? Save the complete path of the image, not only the filename.

Leave a Comment