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:

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:

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

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; 
    $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 != ''){
            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:

<br />
<br />
<br />

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


$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) != ''){
        echo '<br />';
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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