Naderjlyr
Naderjlyr

Reputation: 175

how to list a repeater sub fields with label and value in Advanced Custom Field?

I've searched and I could not find any solution to list a repeater field rows with Label of sub field and its value.

in my case I want to list a repeater field sub fields with Label and value. for example :

'Sub Field Label' = 'Value'

is there any way to do this ?

Upvotes: 1

Views: 3527

Answers (2)

Suresh Suthar
Suresh Suthar

Reputation: 812

 <?php $args = array('post_type' => 'post');
            $the_query = new WP_Query( $args );
            query_posts( $args );
            while ( have_posts() ) : the_post();
   $field = get_field_object('field_name'); 
   echo $field['label']; //print label name
   echo the_field('field_name'); //and its value
    endwhile; wp_reset_query(); ?>

please try this hope help to you

Upvotes: 1

Jordi Nebot
Jordi Nebot

Reputation: 3401

If you know the labels you want to retrieve from your Repeater Field, just use the standard method:

if( have_rows('repeater_field_name') ):
    while ( have_rows('repeater_field_name') ) : the_row();
        echo 'Label = ' . get_sub_field('sub_field_name') . '<br>';
    endwhile;
endif;

If you aren't in a single post/page or outside The Loop, just add the $post_id as the second parameter to your ACF function calls. For example: have_rows('repeater_field_name', $post_id).


If you don't know the label names, I guess you could use get_fields() to get an array of all custom fields for the current post and iterate it. Something like:

$fields = get_fields($post->ID);
foreach ($fields as $field_type => $field) {
    if ( $field_type == 'repeater_field' ) {
        foreach ($field as $row) {
            foreach ($row as $label => $value) {
                // In this case you should be aware that
                // $value could be an Array too...
                echo $label . ' = ' . $value;                    
            }
        }
    }
}

Anyway, I recommend you to take a look at ACF Documentation. It's complete, clear and with lots of code snippets covering the most common uses.

Upvotes: 1

Related Questions