Support

Account

Home Forums Add-ons Repeater Field how to list name of subfields in a row

Solved

how to list name of subfields in a row

  • Im using this code

    <?php
    
    // check if the repeater field has rows of data
    if( have_rows('repeater_field_name') ):
    
     	// loop through the rows of data
        while ( have_rows('repeater_field_name') ) : the_row();
    
            // display a sub field value
            the_sub_field('sub_field_name');
    
        endwhile;
    
    else :
    
        // no rows found
    
    endif;
    
    ?>

    I want to print all the sub_fields values, without knowing their names. how can i do that easily?

    I was trying to populate an array with the repeater object and get it out, but i suck at php.

    thanks for help! 🙂

  • Hi

    instead of using a while loop, if you get the field directly via the get_field function, it will actually return you an associative array with all the sub fields value. Then you can just run a normal php’s foreach on it to do whatever you want.

    
    <?php
        $values = get_field('repeater_field_name')? : [];
        foreach ($values as $key => $value) {
            // do what you want here
        }
    
    

    Cheers 🤓

  • thanks for your help. I still don’t understand it. I tried removing the while loop and using your solution and I’m able to print all the keys and value but onyl from the first row of the array:

    if($check['type'] == 'repeater'):
    									if( have_rows($name) ):
    											$values = get_field_object($name)? : [];
    											foreach ($values as $key => $value) {
           											$content .= "key: $key - value: $value </br>";
       											 }
    									endif;
    								elseif($check['type'] == 'wysiwyg'):
    									$content .= $value;
    								else:
    
    								endif;

    and what I’m actually trying to exactly do is this:

    if( have_rows($name) ):
    										
    										while ( have_rows($name) ) : the_row();
    										
    											// write method to print subfields without knowing their names.
    											$content .= get_sub_field('effect_name') . "</br>";
    											$content .= get_sub_field('effect_magnitude') . "</br>";
    											$content .= get_sub_field('effect_description') . "</br>";
    
    										endwhile;
    									endif;

    but without knowing the names ‘effect_name’, ‘effect_magnitude’ and ‘effect_description’

  • ok i solved this thank you so much. finally it worked for me like this to print all the values in subfields:

    if($check['type'] == 'repeater'):
    									if( have_rows($name) ):
    											while ( have_rows($name) ) : $the_row = the_row();
        											foreach ($the_row as $key => $value) {
            											$content .= "$value </br>" ;
       													 }
       											endwhile; 
    									endif;
    								elseif($check['type'] == 'wysiwyg'):
    									$content .= $value;
    								else:
    
    								endif;
  • Hi vandelay, do you still use this method? and if so any chance of the exported JSON so can see how you have set it up please

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

The topic ‘how to list name of subfields in a row’ is closed to new replies.