0
votes

I'm looking for an easy way to display prepack sizing distribution for wholesale clothing. So, the product page will display a table with how many pieces of that clothing come in each size. Unfortunately, since I'm new here, I can't upload a picture of my ACF fields but I have an ACF group field and then inside that I have subfields with the size's name(Small, Medium, Large, etc.) and then the value is the number of pieces in that size.

I want to loop through the subfields and echo out the label of the subfield and the value of the subfield so that I can make a table. I don't want to have to use specific subfield names because I want the code to work even if I make changes to the names. Here's what I have so far:

$fields = get_field('lettered_packing'); //my parent field
foreach ($fields as $field) {
    echo $field;
}

Doing this echoes out the value of each subfield but I would also like to be able to echo out the label of each subfield as well. Is this possible?

2

2 Answers

0
votes

I was able to find a solution that worked for me. I'll post it here incase someone else comes across this problem.

So, the trick was creating an array from the get_field_object function to get all subfield names and then using that array when going through the parent field to gain access to both value and label.

$parent_field = get_field_object('your-parent-field-name');
    $fields = array();

    if (count($parent_field['sub_fields'])) {
        foreach ($parent_field['sub_fields'] as $field) {
            $fields[] = $field['name']; 
        }
    }
if( have_rows($parent_field) ):
    while ( have_rows($parent_field) ) : the_row(); 

    foreach ($fields as $sub_field) {
        $field = get_sub_field_object($sub_field);
        
        echo '<span>' . $field['label'] . ':' . $field['value'] . '</span>'; 

    }

I'm not sure if this is the most eloquent solution or not. But it works for now.

0
votes
<?php
    // To get "{label} {value}" from acf group:
    // input desired group name 
    $group = 'parent_group_name_goes_here'; 
    if (have_rows($group)) {
        while (have_rows($group)) : $the_row = the_row();
            foreach ($the_row as $field_key => $field_value) {
                // You only get a numeric key and the output value from the_row so...
                // Retrieve the field object with all the meta data using this key:
                $field_object = get_field_object($field_key);
                
                
                // Display the label from get_field_object
                echo $field_object['label'];
                
                /** Other options are :  
                 * ['ID']
                 * ['key']
                 * ['label']
                 * ['name']
                 * ['prefix']
                 * ['type']
                 * ['value']
                 * ['menu_order']
                 * ['instructions']
                 * ['required']
                 * ['id']
                 * ['class']
                 * ['conditional_logic']
                 * ['parent']
                 * ['wrapper'] => ['width'] / ['class'] / ['id']
                 * ['default_value']
                 * ['placeholder']
                 * ['prepend']
                 * ['append']
                 * ['maxlength']
                 * ['_name']
                 * ['_valid']
                 * **/
                 
                // Display the value from the foreach loop
                echo $field_value;
            }
        endwhile;
    }
?>