ThemeShaper Forums » Thematic


conditionally echo <br /> in WPAlchemy meta box

(3 posts)
  • Started 9 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 != '') {
    		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');
    	echo the_content();
    	echo '<div id="artwork-meta">';
    	// Loop through the meta values
    	foreach ($values as $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">
     br  <!-- no value but break still echoes -->
           Additional Info
     br   <!-- also shouldn't be there... -->

    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 9 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')) {
    if($featured_metabox->is_first()){ echo '1st bacon';}
    if($featured_metabox->is_last()){ echo 'last bacon'; }
    } else { echo "you borked it"; }
    Posted 9 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:

    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:

    $values = array('title','collabs','dimen','additional');
    foreach ($values as $val) {
    		while ($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 9 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.