Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

WPAlchemy: How to output values? WordPress

  • SOLVED

I am using [[LINK href="http://www.farinspace.com/wpalchemy-metabox/"]]http://www.farinspace.com/wpalchemy-metabox/[[/LINK]] and have created my meta boxes and the data is being saved successfully (See below) but I don't know how to output these 'ingredients' on the front end?

Array
(
[docs] => Array
(
[0] => Array
(
[ingredient] => Cheese
)
[1] => Array
(
[ingredient] => Salt
)
)
)


<strong>This is my single.php</strong>

<?php
global $simple_mb;
$meta = $simple_mb->the_meta();
?>


<strong>This is my meta.php</strong>

<div class="recipe_ingredients_metabox">

<?php while($mb->have_fields_and_multi('docs')): ?>
<?php $mb->the_group_open(); ?>

<?php $mb->the_field('ingredient'); ?>
<p class="clearfix">
<label>Ingredient</label>
<input class="text" type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/>
<a href="#" class="dodelete button">Remove</a>
</p>

<?php $mb->the_group_close(); ?>
<?php endwhile; ?>

<p style="margin-bottom:15px; padding-top:5px;" class="clearfix"><a href="#" class="docopy-docs button"><?php echo __('Add Ingredient'); ?></a></p>

</div>


<strong>This is my functions.php</strong>

// Meta WP Alchemy
include_once 'metaboxes/setup.php';

$custom_metabox = $simple_mb = new WPAlchemy_MetaBox(array
(
'id' => '_custom_meta_ingredient',
'title' => 'Recipe Ingredients',
'types' => array('recipe'),
'mode' => WPALCHEMY_MODE_EXTRACT,
'prefix' => '_ingredient_',
'autosave' => TRUE,
'template' => get_stylesheet_directory() . '/metaboxes/ingredients-meta.php',
));

Answers (3)

2012-07-12

Jatin Soni answers:

Alright I have just finish one gient project using WPAlchemy metabox script. I love it.

Now as you are using <strong>while</strong> I believe you want to allow multiple entry for the same field. So in this case you have to get value by using <strong>while</strong> too.

So code will be something like this

while($meta->have_fields('docs')) {
//all stuffs you want to dsiplay will goes here.
}



Also in your code
<?php

global $simple_mb;

$meta = $simple_mb->the_meta();

?>


It may not work and if that is the case than try to use this code

Change your above code to this
<?php

global $simple_mb;

$simple_mb->the_meta();

?>


and to get value use below code

while($simple_mb->have_fields('docs')) {
//all stuffs you want to dsiplay will goes here.
}


To get field value use below code

<?php echo $simple_mb->the_value('ingredient'); ?>

so your final code should be something like this

while($simple_mb->have_fields('docs')) {
//all stuffs you want to dsiplay will goes here.
echo $simple_mb->the_value('ingredient');
}




Keith Donegan comments:

Thanks Jatin.

2012-07-12

Romel Apuya answers:


<?php

global $simple_mb;

$meta = $simple_mb->the_value('ingredient');


echo $meta;
?>


Keith Donegan comments:

Thanks Romel but nothing is displayed?

2012-07-12

Arnav Joy answers:

to display a value , use this

$metabox->the_value('name'); // where 'name' is the name of the field..

in your case it will be

<?php



global $simple_mb;

// set current field, then get value

$simple_mb->the_field('ingredient');
$ingredient = $simple_mb->the_value();

echo $ingredient;

?>


Keith Donegan comments:

Your code looked promising but unfortunately it doesn't work :(