Support

Account

Home Forums Front-end Issues if field has value within Field Group conditional statement

Solved

if field has value within Field Group conditional statement

  • I have a field group(product specs) with bunch of sub fields. I want to put the subfields all within a container. Id like a field group if statement, there is none to be found in docs. Making me think am going about it wrong. I can live with an empty table however it feels so wrong.

    Thanks for your help.

    <?php
    
        if(get_field_group('field_group_name'))
        {
        	echo '<table>';
        	if(get_field('field_name'))
        	{
    			echo '<td>' . get_field('field_name') . '</td>';
    		}
    
    		if(get_field('field_name'))
    		{
    			echo '<td>' . get_field('field_name') . '</td>';
    		}
    
    	 echo '</table>';
        }
    
    ?>
  • It should be possible to eliminate this condition but I’d need more to go on. What does your code look like that producing the empty table?

  • My code is a little more complex. I tried to simplify code to get the point across.

    <?php
    
    echo '<table>';
    
    if( get_field('product_watts') ) {
    
        echo '<tr><td>' . '<strong>Wattage: </strong></td><td>'; 
        while ( have_rows('product_watts') ) : the_row();
        	 $array1[] = get_sub_field('watts')  .'w'; 
        endwhile;
        	$watts = implode(', ', $array1);
    
        echo $watts .'</td></tr>'; 
    }
    
    if( get_field('product_watt_replacement') ) {
    
        echo '<tr><td>' . '<strong>Wattage Replacement: </strong></td><td>'; 
        while ( have_rows('product_watt_replacement') ) : the_row();
        	 $array2[] = get_sub_field('watt_replacement')  .'w'; 
        endwhile;
        	$watt = implode(', ', $array2);
    
        echo $watt .'</td></tr>'; 
    }
    
    echo '</table>';
    
    ?>
  • It looks like you have several repeaters rather than a single repeater. You will need to check each repeater to see if it has anything before creating the <table></table>

    There are a couple ways that you can do this

    1

    
    <?php
    
    if( get_field('product_watts') && get_field('product_watt_replacement')) {
    echo '<table>';
    
    if( get_field('product_watts') ) {
    
        echo '<tr><td>' . '<strong>Wattage: </strong></td><td>'; 
        while ( have_rows('product_watts') ) : the_row();
        	 $array1[] = get_sub_field('watts')  .'w'; 
        endwhile;
        	$watts = implode(', ', $array1);
    
        echo $watts .'</td></tr>'; 
    }
    
    if( get_field('product_watt_replacement') ) {
    
        echo '<tr><td>' . '<strong>Wattage Replacement: </strong></td><td>'; 
        while ( have_rows('product_watt_replacement') ) : the_row();
        	 $array2[] = get_sub_field('watt_replacement')  .'w'; 
        endwhile;
        	$watt = implode(', ', $array2);
    
        echo $watt .'</td></tr>'; 
    }
    
    echo '</table>';
    } // end if all fields have values
    
    ?>
    

    The other way would be to loop though each field and build the html and then test to see if there is any content, something like this

    
    <?php
    
    $content = '';
    if( get_field('product_watts') ) {
    
        $content .= '<tr><td>' . '<strong>Wattage: </strong></td><td>'; 
        while ( have_rows('product_watts') ) : the_row();
        	 $array1[] = get_sub_field('watts')  .'w'; 
        endwhile;
        	$watts = implode(', ', $array1);
    
        $content .=  $watts .'</td></tr>'; 
    }
    
    if( get_field('product_watt_replacement') ) {
    
        $content .=  '<tr><td>' . '<strong>Wattage Replacement: </strong></td><td>'; 
        while ( have_rows('product_watt_replacement') ) : the_row();
        	 $array2[] = get_sub_field('watt_replacement')  .'w'; 
        endwhile;
        	$watt = implode(', ', $array2);
    
        $content .=  $watt .'</td></tr>'; 
    }
    
    if ($contend) {
     echo '<table'>',$content,'</table>';
    }
    ?>
    
  • I knew there was a reason no one asked. Simple php logic. Its amazing how far am getting with my lack of skill. Thanks for getting that much farther.

    Both solutions taught me something. What does that “.” after $content before the “=” do?

    Thanks again.

  • $x .= ' add this text'

    is shorthand for

    $x = $x . ' add this text

    In PHP anything that can be done the second way can be done the first, +=, -=, *=, etc.

  • Cool stuff. Thanks for quick response and explanation.

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘if field has value within Field Group conditional statement’ is closed to new replies.