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 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

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 make a Custom Login Page in WordPress?WordPressHow to Use ROLLUP Operator in SQL and MySQL?SQL/MySQLEssential SQL Commands We Need to KnowSQL/MySQLHow to create a pricing table using flex in CSS?CSSFor Each Loop in Swift for BeginnersSwiftFlash of Invisible Text and Mitt Romney Web Font ProblemCSSCheck if any column has NULL values in PostgresPostgresHow to check if the page is single post page in WordPress?WordPressWhat’s new in Constructor in PHP 8?PHPaddcslashes() and addslashes() String Functions in PHPPHPHow to detect the Blog Page in WordPress?WordPressHow to create a Star Ratings using CSS?CSS