Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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="<?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.

share|improve this question

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="coffee_star.jpg" />

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

share|improve this answer
    
yes you are right.. i'm getting <img src="coffee_star.jpg" /> in my HTML code as the filename only not file path. Please tell me the way to get filepath for displaying the image.. –  strange man Dec 10 '12 at 10:02
    
How should I know where your images are stored? WordPress have a lot of functions (codex.wordpress.org/Function_Reference/admin_url#Related) to create a path. Please read the codex for more informations. –  Ralf912 Dec 11 '12 at 9:49
    
thanks for ur reply.. i just found this but cant display the image. Would u please look into this: [wordpress.stackexchange.com/questions/75713/… –  strange man Dec 11 '12 at 10:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.