Home › Forums › General Issues › display list of values from one field group
Hi everyone,
Like in the title. How can I make a list of all values from one field group?
Ex. group name is “stuff”. There is one field “kind_stuff” (repeater) with some sub_fields like: “name”, “cost”, “photo”, “link”, etc.
I want to display it in one table. Can anybody help with that?
thanks
Paul
Hi @bispawel,
The important thing here is the repeater aspect, as opposed to the group itself. So within your post/page you’ll have a repeater that contains say 10 rows of data. It works a little bit like the WordPress loop. First, you check to see if there’s any data at all, then if there is data, you start a while loop to be able to access each repeater row individually.
Because these are sub_fields you need to use get_sub_field() or the_sub_field() to reference that data. In practice, it should look a little bit like this:
<?php
// check if the repeater field has rows of data
if( have_rows('kind_stuff') ):
?>
<table>
<?php
// loop through the rows of data
while ( have_rows('kind_stuff') ) : the_row();
// attribute sub fields to variables
$stuff_name = get_sub_field('name');
$stuff_cost = get_sub_field('cost');
$stuff_photo = get_sub_field('photo');
$stuff_link = get_sub_field('link');
?>
<tr>
<td><img src="<?php echo $stuff_photo['url']; ?>" alt="<?php echo $stuff_photo['alt']; ?>" /></td>
<td><?php echo $stuff_name; ?></td>
<td><?php echo "$" . $stuff_cost; ?></td>
<td><a href="<?php echo $stuff_link; ?>">View product</a></td>
</tr>
<?php endwhile; ?>
</table>
<?php
else :
// no rows found
endif;
?>
Depending on how you’ve set up each of those sub_fields will depend on specifically how you echo them out.
edit: The Repeater documentation page may help you further: https://www.advancedcustomfields.com/resources/repeater/
Hi Edd,
thanks so much! Thats work great. But what if I want display all values? Witch are all in a different post/page or these values are in one category ex. “store”?
Paul
Assuming i follow your meaning, you’d have to also wrap that snippet of code above with a WP_Query or query_posts call for all the posts that have table data. This could be quite simple if they’re all in one category or of a particular post type, otherwise you may need to get more specific with a meta query, which will allow you to specifically call in all posts that have the photo sub_field filled in, for example.
If you can give me an idea of the specific heirarchy of the site setup or what the post type is called or the category name I can try to help with your specific query
This may be a bit flakey as i’m typing on a phone, but perhaps something like the following:
$query_store_args = array(
'post_type' => 'posts',
'cat' => 'store',
'posts_per_page' => -1
);
$query_store = new WP_Query($query_store_args);
if( $query_store->have_posts() ) :
while( $query_store->have_posts() ) : $query_store->the_post();
// code I added above
endwhile;
endif;
Edit: forgot a line
Yes yes yes…! 🙂
This is what I need – Edd You are great ! :]
thanks so much
Paul
The topic ‘display list of values from one field group’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.