YogeshChauhan . com

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; 
?>


amazon

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 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

What is Conditional Rendering in React?ReactExplanation of PostgreSQL PgAdmin interfacePostgresCreate a menu with a curtain falling animation using CSS and JavaScriptCSSLearn to create your skill bar using CSSCSSWindow setInterval() Method in JavaScriptJavaScriptThe Sort and Compare Functions in JavaScriptJavaScriptWhat’s a Strict mode in JavaScript?JavaScriptfirst-of-type and last-of-type selectors in CSSCSSHow to load variables, functions, and mixins from another module in SCSS?SCSSLearn to Establish Connection using MySQLi (object-oriented), MySQLi (procedural) and PDO with Example CodePHPHow to Secure Web or Mobile Browsers?MiscSQL GROUP BY StatementSQL/MySQL