Support

Account

Home Forums Add-ons Repeater Field loop to display all subfield of a repeater

Solved

loop to display all subfield of a repeater

  • Hello

    I create a “social network” repeater. Inside we have facebook, instagram …. some of them will be use by the user or not

    I wish display dynamicaly the subfield if they exist. I thought we have to you get field object.

    if( have_rows('gerants', 'option') ):
       while ( have_rows('gerants', 'option') ) : the_row(); 
         if( have_rows('reseaux-sociaux', 'option') ):
           while ( have_rows('reseaux-sociaux', 'option') ) : the_row(); 
    											 
              $fields = get_sub_field_object();
    
               if( $fields ){												                      
                  foreach( $fields as $field ){
    													 
                    echo '<ul>';														        
                    echo '<li>' . $field['label'] . ': '.$field['value'].'</li>';												 
                    echo '</ul>';
    												                            
                  }
    	  }
    
           endwhile;
         endif;
       endwhile;
    endif;

    but the problem is get_sub_field_object() seems to need an argument as the name of the field // or the ID. In my case I need make something generic so maybe the type of the field ?

    There is a way to do it ?

  • If you don’t already know the names (or field keys) of the sub fields then that’s going to be an issue. You have to get these, this might work, but no promises as I have not tested it.

    
    if( have_rows('gerants', 'option') ):
       while ( have_rows('gerants', 'option') ) : the_row(); 
    	 
    	   // get the sub field object of the nested repeater
    		 // and store a list of sub fields
    	 	 $reseaux_sociaux = get_sub_field_object('reseaux-sociaux');
    		 $fields = array();
    		 if (count($reseaux_sociaux['sub_fields'])) {
    			 foreach ($reseaux_sociaux['sub_fields'] as $field) {
    					$fields[] = $field['name']; 
    			 }
    		 }
         if( have_rows('reseaux-sociaux', 'option') ):
           while ( have_rows('reseaux-sociaux', 'option') ) : the_row(); 
    		   	 echo '<ul>';
    					foreach ($fields as $sub_field) {	
    						$field = get_sub_field_object($sub_field);
    						echo '<li>' . $field['label'] . ': '.$field['value'].'</li>';			
    					}
    				echo '</ul>';
           endwhile;
         endif;
       endwhile;
    endif;
    
  • Thanks but it’s displaying all the fields in reseaux-sociaux even if they are empty

  • Hello John

    Have you tested something to resolve it ?

  • A have_rows() loop will display empty rows unless you check to see if every sub field has a value first. In a normal loop it would be something like

    
    if (get_sub_field('field1') && get_sub_field('field2') /* && etc... */) {
      // all field have content, output content
    }
    
  • thanks 🙂 it’s working

    the final code:

    
     while ( have_rows('reseaux-sociaux', 'option') ) : the_row(); 
    		   	 echo '<ul>';
    					foreach ($fields as $sub_field) {	
    						$field = get_sub_field_object($sub_field);
    						$lien = $field['value'];
    						$image =  $field['label'];
    						if ($field['value']){
    						echo '<li><a href="'.$lien.'" target="_blank"><img src="'.get_stylesheet_directory_uri().' /imgs/icons/social/rondes/pleines/' . $image. '-icon.svg"></a></li>';
    						}			
    					}
    				echo '</ul>';
           endwhile;
Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.