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 working on a Thematic child theme and using the WPAlchemy Meta Box Class to create an 'Artwork Info' meta box that I want to conditionally echo like this at the end of each post:

Title
Medium
Dimensions
Additional Info

My instance of the class is defined like this in functions.php:

$prefix = 'wpf_';
$artinfo_mb = new WPAlchemy_MetaBox(array
(
    'id' => '_custom_meta', // underscore prefix hides fields from the custom fields area
    'title' => 'Artwork Info',
    'template' => STYLESHEETPATH . '/custom/artwork-meta.php',
    'context' => 'normal',
));

And here's an example of the HTML for each field, located in artwork_meta.php:

<label>Title</label> 
<p>
    <?php $mb->the_field('title'); ?>
        <input type="text" style="width:99%" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/> 
</p>

I am using this function in functions.php to print the data in each post via an array of the fields:

function display_artwork_info() {

    global $artinfo_mb; 
    $artinfo_mb->the_meta();
    $values = array('title','medium','dimen','additional'); 

    echo the_content();  

    // loop through and conditionally echo the value with a line break
    foreach ($values as $val) {
        if ($val != ''){
            $mb->the_value($val);
            echo '<br />';
        }
    } 
} 
add_action('thematic_post', 'display_artwork_info');

All works fine except the foreach ($values as $val) always echoes the line breaks, even if the field's value contains no data. For example, if the 'medium' and 'additional info' fields are empty, the HTML echoes like this:

Title
<br />
<br />
Dimensions
<br />

Is there something wrong with my loop? Am I using the wrong WPAlchemy function to echo the meta data? Thanks in advance :)

share|improve this question

1 Answer 1

up vote 2 down vote accepted

$val will never == '' here, because $val is holding your field name, not the data. also, use get_the_value to have it returned, the_value will just echo the data out.

foreach ($values as $val) {
    if ($artinfo_mb->get_the_value($val) != ''){
        $artinfo_mb->the_value($val);
        echo '<br />';
    }
} 
share|improve this answer
    
Perfect...thanks so much! –  notlaura Apr 24 '11 at 18:02

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.