0

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 :)

1 Answer 1

2

$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 />';
    }
} 
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.