Yogesh Chauhan's Blog

WordPress: How to loop through ACF group fields?

in WordPress on June 3, 2021

The group field helps you group multiple fields.

For e.g. if you want to add a link with a label and an URL then you would want to group them together and make the whole group field a required field. That way, admin user won’t miss any of those fields.

ACF group field helps in organizing your fields as well as makes the UI a bit more simplified.

When you save a group field, it saves both child and parent field names. For e.g. if a you name a group field as ‘parent’ and a sub field as ‘child’ then it will be saved as ‘parent_child’ in the database. You can load it using the same name.

ACF Group Field Example

Here are some screenshots from ACF official docs.

Group field that displays multiple fields (image, link, caption, etc.) within
List of settings shown when creating a Group field

This is how we can loop through those fields:


<?php 
  
  if( have_rows('hero') ):

    while( have_rows('hero') ): the_row(); 

        // Get sub field values.
        $image = get_sub_field('image');
        $link = get_sub_field('link');

        // get the meta
        $image_url = $image['url'];
        $image_alt = $image['alt'];
        $link_url = $link['url'];
        $link_title = $link['title'];

        //do something 

    endwhile; 

  endif; 
?>



Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How to use data-* Attributes in HTML?HTMLData Flows in ReactReacttransform-origin Property in CSSCSSHow to Secure Web or Mobile Browsers?MiscellaneousWhat happened to your Google Analytics Tracking ID, can’t find it?MiscellaneousHow does @extend rule work in SCSS (Sass)?SCSSHow to Validate User Name, Email Address and URL in PHP?PHPHow to check if the page is the home page in WordPress?WordPressHow to redirect or load a new document using JavaScript?JavaScriptCreate a menu with a curtain falling animation using CSS and JavaScriptCSSShould we ever delete data from a database?MiscellaneousHow different is Handling Events in React vs HTML DOM?React