Home › Forums › General Issues › How to get field data from a group inside a group
I have created a group field album_group
that includes groups inside it. What I’m trying to figure out now is how to output the text fields inside album_information
. For example, get the campaign_title
field using the usual <?php echo get_field('campaign_title');?>
Album Group
Album Information
Am I correct in saying I can access the nested group fields without having to use a loop? I’d rather just access them so I can place in certain divs etc.
All good, worked it out by putting it through the loop then accessing the sub_fields!
<?php
if( have_rows('album_group') ):
while( have_rows('album_group') ): the_row();
$cp_album_info = get_sub_field('album_information');
$cp_album_music = get_sub_field('album_music_g');
$cp_album_name = get_sub_field('campaign_album_name');
$cp_album_links = get_sub_field('album_links');
...
?>
I had the same problem and it drove me nuts. This should be mentioned in the docs somewhere. I know it is on the groups page of the docs, but it does not state WHY.
Using the variables isn’t working for me, neither is the basic code from the documentation. I also tried the loop have_rows() function but with no success. Can someone help me?
<?php
// vars
$phone = get_field(‘contact_phone’);
if( $phone ): ?>
<p class=”heading-8″><?php echo $phone[‘title’]; ?></p>
<?php endif; ?>
I’m using this documentation as reference and like I said, neither of those methods are working.
https://www.advancedcustomfields.com/resources/group/
<div class="war-front-form">
<?
php if( have_rows('warranty-member') ): //parent group field
while( have_rows('warranty-member') ): the_row();
// vars
$name = get_sub_field('warranty-name');
$birth = get_sub_field('warranty-birth');
$phone = get_sub_field('warranty-phone');
$email = get_sub_field('warranty-email');
?>
<div class="war-front-member">
<h1>고객정보</h1>
<ul>
<li><span>이름</span><p><?php echo $name ?></p></li>
<li><span>생년월일</span><p><?php echo $birth ?></p></li>
<li><span>연락처</span><p><?php echo $phone ?></p></li>
<li><span>이메일</span><p><?php echo $email ?></p></li>
<li>
<?php if( have_rows('warranty-adress-group') ): //child group field
while( have_rows('warranty-adress-group') ): the_row();
// vars
$postcode = get_sub_field('warranty-postcode');
$adress = get_sub_field('warranty-adress');
$adress2 = get_sub_field('warranty-adress-2');
?>
<span>주소</span>
<p><?php echo $postcode ?></p>
<p><?php echo $adress ?></p>
<p><?php echo $adress2 ?></p>
<?php endwhile; ?>
<?php endif; ?>
</li>
</ul>
</div>
<?php endwhile; ?>
<?php endif; ?>
this is my group in group source code
If you don’t need to use loops, this will work for you.
<?php
$our_services = get_field('our_services'); // 'our_services' is your parent group
$service_one = $our_services['service_one']; // 'service_one' is your child group
?>
<?php echo $service_one['service_heading']; // 'service_heading' is a subfield of your child group ?>
`<?php $bigImageGroup = get_field(‘big_image_with_button’);
?>
<img src=”<?php echo $bigImageGroup[‘image’][‘url’]; ?>” alt=”<?php echo $bigImageGroup[‘image’][‘alt’]; ?>” width=”100%”>
<a href=”<?php echo $bigImageGroup[‘button’][‘url’]; ?>” class=”btn secondary”><?php echo $bigImageGroup[‘button’][‘text’]; ?></a>
This worked for me, I’m not well versed but it seems on PHP but it seems that you keep passing on the objects.
I like to add a helper function to my themes…
if ( function_exists( 'get_field' ) ) {
function get_group_field( string $group, string $field, $post_id = 0 ) {
$group_data = get_field( $group, $post_id );
if ( is_array( $group_data ) && array_key_exists( $field, $group_data ) ) {
return $group_data[ $field ];
}
return null;
}
}
Usage: <?php echo get_group_field( 'album_group', 'album_information' ); ?>
Thanks for your solution. How would this function look like if ‘album_information’ is another group width fields in it? Like:
get_group_field( 'album_group', 'album_information', 'album_title' )
The topic ‘How to get field data from a group inside a 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.