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 include CSS and JavaScript files in your WordPress plugin?WordPressALTER DATABASE in PostgreSQLPostgresComposing and Extracting Components in ReactReact4 ways to create Date Objects in JavaScriptJavaScriptHow to refresh a page using PHP at regularly occurring intervals?PHPHow to apply style to a specific child element using CSS?CSSWhat is Etrieve Flow?MiscellaneousHow to Use SQL MAX() Function with Dates?SQL/MySQLHow to create a sticky menu pin using CSS?CSSAngular 9 time clock update every minute, second, hourAngularHow to Remove PHP File Extensions From Your Website URLs?PHPHow to create a cross-browser smooth scrolling with jQuery?jQuery