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 🙂
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:It seems that the plugin is using post meta so you should need something like this:
But rewritten to save the data as a variable instead of
echo
it. Guessing based on the code you posted, I believe you need:Untested, but stands a good chance of working.
Try this:
Assumes hjemmeside is the name of your custom field key.