ThemeShaper Forums » Thematic

[closed]

conditionally echo <br /> in WPAlchemy meta box

(3 posts)
  • Started 3 years ago by notlaura
  • Latest reply from notlaura
  • This topic is not resolved
  1. Hi - I am working on an 'Artwork Info' meta box created with the WPAlchemy MetaBox PHP Class. In my child theme's functions.php I am using the following to loop through and print the field values:

    // Print the meta box data - called in display_artwork_info()
    function print_meta($val){
    	global $artworkinfo_metabox;
    	if($val != '') {
    		$artworkinfo_metabox->the_value($val);
    		echo "br";
    	}
    }
    
    // Display Artwork Info fields in post
    function display_artwork_info() {
    
    	global $artworkinfo_metabox;
    	global $post;
    	global $terms;
    
    	$values = array('title','collabs','dimen','additional');
    	$artworkinfo_metabox->the_meta();
    
    	echo the_content();
    	echo '<div id="artwork-meta">';
    
    	// Loop through the meta values
    	foreach ($values as $val){
    		print_meta($val);
    	}
    
    	echo '</div>';
    } 
    
    add_action('thematic_post', 'display_artwork_info');

    This all works well, except if one or all of the fields are empty, the line breaks still echo. With one field missing, the HTML comes out like this:

    <div id="artwork-meta">
          Title
     br  <!-- no value but break still echoes -->
     br
           Dimensions
     br
           Additional Info
     br   <!-- also shouldn't be there... -->
    </div>

    Any ideas? Thanks in advance.

    P.S. I'm writing br instead of the actual tag because the forum applies it as a tag.

    Posted 3 years ago #
  2. gosh i love alchemy. it is freaking brilliant.

    i'm not sure why you need the extra print_meta function?

    if you set up a repeating field, like have_fields or have_fields_and_multi you can just loop through it using the wpalchemy object. here's a sample of something from i posted in his comments (that's me Kathy)

    if ($featured_metabox->have_value('slides')) {
    
    while($featured_metabox->have_fields('slides')){
    if($featured_metabox->is_first()){ echo '1st bacon';}
    $featured_metabox->the_value('url');
    if($featured_metabox->is_last()){ echo 'last bacon'; }
    }
    } else { echo "you borked it"; }
    Posted 3 years ago #
  3. I was using print_meta() to print the break because it wouldn't print in the loop for some reason. I'd like the fields to echo like this - a line break after each printed value, not just first/last:

    Title
    Medium
    Dimensions
    Collaborators
    Additional info

    In your function, 'slides' is the title of WPAlchemy_MetaBox() and 'url' is the name of a field, right? I think there may be a gap in my understanding somewhere. I shortened my loop to this, which prints everything but not the break:

    $mb->the_meta();
    $values = array('title','collabs','dimen','additional');
    
    foreach ($values as $val) {
    		while ($mb->the_value($val) != ''){
    			$mb->the_value($val);
    			echo 'break';
    		}
    }

    So, the output is something like

    The TitleThe MediumThe DimensionsThe CollaboratorsAdditional info

    So, I'd made the print_meta() function to compensate for that, but it always printed the break instead of conditionally.

    Thanks a lot for your help!

    Posted 3 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.