Support

Account

Home Forums General Issues Detect if field is hidden by conditional logic

Solved

Detect if field is hidden by conditional logic

  • Hello, I am making a field group where some fields are hidden depending on user choice (radio button + conditional logic). Foe example one field group is displayed on Page edit screen. User decides by using radio button field if he wants to display footer content or not (3 other fields which are hidden/shown by conditional logic). For example when user deicdes to display footer and fills footer fields values the footer is displayed with those values (and thats ok) but when user after that decides to hide footer with those 3 fields they are hidden on admin edit screen but get_field() is still finding them in database and displaying their values so in front end they are not hidden! How to detect if field should be hidden?
    Same thing happens when I attach field group to page, fill all values and then detach field group – even that thosae values are no longer visible and editable they are still returned by get_field() and displayed on my website.

  • You could set it up like this (I think):

    “display_footer” => Field Type = True/False
    “footer_col_1” => Field Type = Text Area/Editor
    “footer_col_2” => Field Type = Text Area/Editor
    “footer_col_3” => Field Type = Text Area/Editor

    With this code:

    <?php
    
    	$footer = get_field('display_footer' );
    
    	if($footer)
    		{
    
    		$footer_col_1 = get_field('footer_col_1' );
    		$footer_col_2 = get_field('footer_col_2' );
    		$footer_col_3 = get_field('footer_col_3' );
    
    		echo '<h3>Footer</h3>';
    		echo '<ul>';
    
    		echo '<li>' . $$footer_col_1 . '</li>';
    		echo '<li>' . $$footer_col_2 . '</li>';
    		echo '<li>' . $$footer_col_3 . '</li>';
    
    		echo '</ul>';
    		}
    
    ?>
  • Yeah this could be it… although I expected something more like get_field() returning null or false in this situation.

  • You can also name a select field $footer with options yes/no. Then just do a conditional statement based on the $footer value.

    <?php
    
    if(get_field('$footer') == "yes")
    {
        //...
    } else {
    
       //...
    }
    
    ?>

    or just

    <?php if( get_field('$footer') ): ?>
    	 //...
    <?php endif; ?>

    As far as returning null, you can skip the option to show the footer and just check if data has been entered into each of the footer column fields. If no, the field wont show.

    <?php 
      
      $col_1 = get_field('$footer_col_1');
      $col_2 = get_field('$footer_col_2');
      $col_3 = get_field('$footer_col_3');
    
      if($col_1) {
    	echo $col_1;
      }
    
      if($col_2) {
    	echo $col_2;
      }
    
      if($col_3) {
    	echo $col_3;
      }
    
    ?>

    Lots of options, so just go with the method that works best for you.

  • Yeah I think that’s it. Thank you for help. By the way is there any way I can check if a field’s field group is attached to a given post/page or get given field’s conditional logic statements?

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

The topic ‘Detect if field is hidden by conditional logic’ is closed to new replies.