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 have some problems displaying info from my custom meta box in my custom post type single. I'm using Reusable Custom Wordpress Meta Boxes by Tammy Hart.

I'm able to display the textfields using this:

<?php echo get_post_meta($post->ID, $prefix.'hjemmeside', true); ?>

But I can't get the image to display, instead DEBUG is telling me that it is an "Undefined variable: post_meta_data in". Currently I'm using this script:

<?php
    $custom_image = $post_meta_data['image'][0];
    echo wp_get_attachment_image($custom_image, 'thumbnail');
?>

Is this wrong? ID for the image meta field is "image".

This is my functions.php file (Only the meta box part):

include (TEMPLATEPATH . '/metaboxes/meta_box.php');

$prefix = 'sample_';

$fields = array(
array( // Hjemmeside
    'label' => 'Hjemmeside', // <label>
    'desc'  => 'Skriv inn hjemmesiden til butikken her.', // description
    'id'    => $prefix.'hjemmeside', // field id and name
    'type'  => 'text' // type of field
),
array( // Facebook
    'label' => 'Facebook', // <label>
    'desc'  => 'Skriv inn facebookadressen til butikken her.', // description
    'id'    => $prefix.'facebook', // field id and name
    'type'  => 'text' // type of field
),
array( // Telefon
    'label' => 'Telefon', // <label>
    'desc'  => 'Skriv inn telefonnummeret til butikken her.', // description
    'id'    => $prefix.'telefon', // field id and name
    'type'  => 'text' // type of field
),
array( // Mailadresse
    'label' => 'Mailadresse', // <label>
    'desc'  => 'Skriv inn mailadressen til butikken her.', // description
    'id'    => $prefix.'mailadresse', // field id and name
    'type'  => 'text' // type of field
),
array( // Senterbeliggenhet
    'label' => 'Senterbeliggenhet', // <label>
    'desc'  => 'Skriv inn hvor butikken er plassert i senteret.', // description
    'id'    => $prefix.'senterbeliggenhet', // field id and name
    'type'  => 'textarea' // type of field
),
array( // Logo
    'label' => 'Logo', // <label>
    'desc'  => 'Last opp logoen til butikken her.', // description
    'id'    => $prefix.'image', // field id and name
    'type'  => 'image' // type of field
),
);

/**
 * Instantiate the class with all variables to create a meta box
 * var $id string meta box id
 * var $title string title
 * var $fields array fields
 * var $page string|array post type to add meta box to
 * var $js bool including javascript or not
 */
$sample_box = new custom_add_meta_box( 'sample_box', 'Butikkinformasjon', $fields, 'butikker', true );

Also, here's the link to the meta_box.php file, which holds all the codes relevant to the meta boxes :)

Here's the output of var_dump(get_post_custom($post->ID));

array(11) { 
  ["_edit_last"]=> array(1) { 
    [0]=> string(1) "1" 
  } 
  ["_edit_lock"]=> array(1) { 
    [0]=> string(12) "1363962761:1" 
  } 
  ["_thumbnail_id"]=> array(1) { 
    [0]=> string(2) "58" 
  } 
  ["sample_text"]=> array(1) { 
    [0]=> string(11) "99 88 99 88" 
  } 
  ["sample_image"]=> array(1) { 
    [0]=> string(1) "0" 
  } 
  ["sample_hjemmeside"]=> array(1) { 
    [0]=> string(21) "www.ethic-clinique.no" 
  } 
  ["sample_facebook"]=> array(1) { 
    [0]=> string(31) "www.facebook.com/ethic-clinique" 
  } 
  ["sample_telefon"]=> array(1) { 
    [0]=> string(11) "99 88 99 88" 
  } 
  ["sample_mailadresse"]=> array(1) { 
    [0]=> string(22) "post@ethic-clinique.no" 
  } 
  ["sample_senterbeliggenhet"]=> array(1) { 
    [0]=> string(42) "Tredje butikken til høyre i andre etasje." 
  } 
  ["sample_logo"]=> array(1) { 
    [0]=> string(42) "Tredje butikken til høyre i andre etasje." 
  } 
}

New var_dump:

array(12) { 

        ["_edit_last"]=> array(1) { [0]=> string(1) "1" } 
        ["_edit_lock"]=> array(1) { [0]=> string(12) "1363964314:1" } 
        ["_thumbnail_id"]=> array(1) { [0]=> string(2) "58" } 
        ["sample_text"]=> array(1) { [0]=> string(11) "99 88 99 88" } 
        ["sample_image"]=> array(1) { [0]=> string(2) "58" } 
        ["sample_hjemmeside"]=> array(1) { [0]=> string(21) "www.ethic-clinique.no" } 
        ["sample_facebook"]=> array(1) { [0]=> string(31) "www.facebook.com/ethic-clinique" } 
        ["sample_telefon"]=> array(1) { [0]=> string(11) "99 88 99 88" } 
        ["sample_mailadresse"]=> array(1) { [0]=> string(22) "post@ethic-clinique.no" } 
        ["sample_senterbeliggenhet"]=> array(1) { [0]=> string(42) "Tredje butikken til høyre i andre etasje." } 
        ["sample_logo"]=> array(1) { [0]=> string(42) "Tredje butikken til høyre i andre etasje." } 
        ["sample_repeatable"]=> array(1) { [0]=> string(74) "a:1:{i:0;a:3:{s:5:"image";s:2:"89";s:5:"title";s:0:"";s:4:"desc";s:0:"";}}" } 

}

Appreciate any help on this :)

share|improve this question
1  
Where did you define $post_meta_data? I suppose nowhere - the error message is exactly stating the problem... –  Johannes Pille Mar 22 '13 at 12:53
    
You're right, I don't think I have defined '$post_meta_data' anywhere. Thinking it was taken care of in meta_box.php that came with the zip. Do you know how I can fix this? Or maybe explain the different parts of the script so I understand it better? :) –  turtledropbomb Mar 22 '13 at 13:19

1 Answer 1

I don't know exactly how that plugin works, and you have posted code that is probably missing context but you need to define $post_meta_data before running line:

$custom_image = $post_meta_data['image'][0];

It seems that the plugin is using post meta so the you should need something like this:

<?php echo get_post_meta($post->ID, $prefix.'hjemmeside', true); ?>

But rewritten to save the data as a variable instead of echo it. Guessing based on the code you posted, I believe you need:

$post_meta_data = get_post_meta($post->ID, $prefix.'image', true);
if (!empty($post_meta_data[0])) {
  $custom_image =  wp_get_attachment_image($post_meta_data[0], 'thumbnail');
}
echo $custom_image; // if desired

Untested, but stands a good chance of working.

share|improve this answer
    
Thanks for helping me out :) I'm really unsure how to define post_meta_data. I've been searching nonstop for several hours trying to find a way to do this, but I can't seem to get it to work. So without knowing the results if I had defined (?) post_meta_data i tried your solutions: The rewritten code gave an illegal string offset and the echo didn't give me the image, but a "0". You know what that means? –  turtledropbomb Mar 22 '13 at 14:12
    
Please post complete error messages. They should identify which line is problematic. I will edit my answer to include a debugging line, or two. –  s_ha_dum Mar 22 '13 at 14:16
    
string(1) "0" Warning: Illegal string offset 'image' in /Applications/MAMP/htdocs/wordpress/wp-content/themes/torvgaarden/single-butikke‌​r.php on line 29(or rather line 3) –  turtledropbomb Mar 22 '13 at 14:19
    
$prefix.'image' or $post->ID is wrong. I assume the former, but it matches the pattern that you claim works for text fields. Try var_dump(get_post_custom($post->ID)); and edit the output into your question. –  s_ha_dum Mar 22 '13 at 14:23
    
Did I understand you right if you told me to not use any of the scripts you suggested, but instead change it with <?php var_dump(get_post_custom($post->ID)); ?>? In that case I got a pretty long line with text.. :) –  turtledropbomb Mar 22 '13 at 14:35

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.