Support

Account

Home Forums Add-ons Repeater Field Display repeater array values

Solved

Display repeater array values

  • Hello,
    I’ve searched first, but haven’t found a straightforward answer as to how to display an array among regular sub-fields. My checkbox array is stored under “expertise”. Here’s my code?

    
    <?php while( have_rows('person') ): the_row();	
    $image_url = get_sub_field('headshot');
    $name = get_sub_field('name');
    $credentials = get_sub_field('credentials');
    $expertise = get_sub_field('expertise');
    $link = get_sub_field('link');
    ?>
    						
    <div class="person text-center">
    <div class="col-sm-4 col-xs-12">
    
    <div class="headshot-container">	
    <img src="<?php echo $image_url; ?>" alt="<?php echo $name; ?>" />
    </div>
    <div class="name">
    <?php echo $name; ?>
    </div>
    
    <div class="credentials">
    <?php echo $credentials; ?>
    
    <div class="expertise">
    Expertise:
    <?php echo $expertise; ?>
    </div>
    
    <a href="<?php echo $link; ?>" class="link">View Profile</a>
    
    </div>
    </div>
    			
    <?php endwhile; ?>
  • Hi @phantasmix

    I’m sorry that I don’t understand your issue. Could you please explain it in more detail and show me the result you want?

    If you want to get the repeater field as an array, you can try this code:

    $person_repeater = get_field('person');
    print_r($person_repeater);

    This page should give you more idea about it: https://www.advancedcustomfields.com/resources/debug/.

    Thanks!

  • Hi,
    sorry, should’ve been more clear.

    Everything works except the field that contains checkbox values outputs the word “array” and not the actual values.

  • Hi @phantasmix

    Thanks for the explanation.

    Checkbox field is always returned as an array, so you can’t use echo to show it. Please take a look at this page to learn more how to use a checkbox field: https://www.advancedcustomfields.com/resources/checkbox/.

    I hope this helps!

  • James,
    I don’t understand what has to go in place of [‘value’] and [‘choices’] in this example:

    <?php 
    $field = get_field_object('field_name');
    $value = $field['value'];
    $choices = $field['choices'];
    
    if( $value ): ?>
    <ul>
    <?php foreach( $value as $v ): ?>
    <li>
    <?php echo $choices[ $v ]; ?>
    </li>
    <?php endforeach; ?>
    </ul>
    <?php endif; ?>

    Should I leave them as-is?

    Thank you

  • Hi @phantasmix ,

    You should also replace the ‘field_name’ with your respective field name.

    The rest should just remain as it is.

    Hope this helps.

  • I did replace ‘field_name’ but now everything disappeared, nothing at all is showing.

    <main id="main" class="site-main" role="main">	
    <?php
    while ( have_posts() ) : the_post();
    
    get_template_part( 'template-parts/content', 'page' );
    
    // If comments are open or we have at least one comment, load up the comment template.
    if ( comments_open() || get_comments_number() ) :
    comments_template();
    endif;
    
    endwhile; // End of the loop.
    ?>
    
    <?php while( have_rows('person') ): the_row();
    $image_url = get_sub_field('headshot');
    $name = get_sub_field('name');
    $credentials = get_sub_field('credentials');
    $link = get_sub_field('link');
    				
    $expertise = get_field_object('expertise');
    $value = $field['value'];
    $choices = $field['choices'];
    if( $value ):
    ?>
    						
    <div class="person">
    <div class="col-sm-4 col-xs-12 text-center">
    <div class="headshot-container">
    <img src="<?php echo $image_url; ?>" alt="<?php echo $name; ?>" />
    </div>
    <div class="name">
    <?php echo $name; ?>
    </div>
    <div class="credentials">
    <?php echo $credentials; ?>
    </div>
    <div class="expertise">
    Expertise:
    <?php foreach( $value as $v ): ?>
    <div><?php echo $choices[ $v ]; ?></div>
    <?php endforeach; ?>
    </div>
    
    <a href="<?php echo $link; ?>" class="link">View Profile</a>
    </div>
    </div>
    <?php endif; ?>
    <?php endwhile; ?>
    
    </main><!-- #main -->
  • Hi @phantasmix,

    That is quite odd

    Kindly share with me some temporary login credentials to your site so that I may login and try to help.

    Hope to hear from you soon.

  • This reply has been marked as private.
  • This reply has been marked as private.
  • Hi @phantasmix

    I’m afraid you disabled the editor so I couldn’t check and modify the template.

    Regarding your issue, could you please change the get_field_object() function to get_sub_field_object() like the following?

    $expertise = get_sub_field_object('expertise');
    $value = $field['value'];
    $choices = $field['choices'];

    I hope this helps!

  • Oops! Sorry, editor now enabled.

    I also just tried with the added _sub, but no luck.

  • Hi @phantasmix

    I’ve just checked your checkbox field, and it seems it has the same labels and values. This makes the code much simpler. You can just get the subfield value like before:

    $expertise = get_sub_field('expertise');

    And then loop it like this:

    <?php foreach( $expertise as $value ): ?>
    	<div><?php echo $value; ?></div>
    <?php endforeach; ?>

    Please check it out. I hope this helps.

  • Fantastic! Thank you 🙂

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

The topic ‘Display repeater array values’ is closed to new replies.