admin管理员组

文章数量:1122846

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/ethic-clinique" 
  } 
  ["sample_telefon"]=> array(1) { 
    [0]=> string(11) "99 88 99 88" 
  } 
  ["sample_mailadresse"]=> array(1) { 
    [0]=> string(22) "[email protected]" 
  } 
  ["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/ethic-clinique" } 
        ["sample_telefon"]=> array(1) { [0]=> string(11) "99 88 99 88" } 
        ["sample_mailadresse"]=> array(1) { [0]=> string(22) "[email protected]" } 
        ["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 :)

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) "[email protected]" 
  } 
  ["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) "[email protected]" } 
        ["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 edited Mar 28, 2013 at 0:18 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Mar 22, 2013 at 12:15 turtledropbombturtledropbomb 2257 silver badges16 bronze badges 2
  • 1 Where did you define $post_meta_data? I suppose nowhere - the error message is exactly stating the problem... – Johannes Pille Commented Mar 22, 2013 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 Commented Mar 22, 2013 at 13:19
Add a comment  | 

2 Answers 2

Reset to default 0

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 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.

Try this:

get_post_meta(get_the_id(),'hjemmeside', true); 

Assumes hjemmeside is the name of your custom field key.

本文标签: custom post typesDisplaying Meta Box Image