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 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to create ‘share on LinkedIn’ link using just HTML?HTMLHow to compress images with gulp in WordPress?WordPressWhat is iFrame in HTML? Why do we need it?HTMLA complete guide to add responsive YouTube videos using HTML and CSSCSSHow to create a Star Ratings using CSS?CSSWordPress: How to set WP_SITEURL?WordPressWhat is Host Hardening and What are some Important Hardening Steps?MiscellaneousMicroservices vs Monolithic ArchitectureMiscellaneousSQL Inner JoinSQL/MySQLObservation of Human Behavior [Shopping Observation Example]MiscellaneousHow to change perception and behavior of a person for acceptance of new systems?MiscellaneousHow to enable and disable button using JavaScript?JavaScript