wordpress codes: advanced custom fields and grouping select repeater fields

Here’s an example of a repeater field on WordPress:

image

Basically, I wanted the page to render the service names under the similarly-labeled service labels.

My brain isn’t very good with the whole PHP logic thing, so I got the internet to help me out. I adapted this original response on StackOverflow and edited it to fit the Advanced Custom Fields code.

$options = get_field(‘products_and_services’);

foreach( $options as $k => $v ){

/* check for services */

if( $label!=$v[‘service_label’] ){

/* New Service */

if( $label!=false ){

/* Close the Previous Service */

echo ‘</ul>’;

}

/* Open the New Service */

echo ‘<h3>’ . $v[‘service_label’] . ‘</h3><ul>’;

$label = $v[‘service_label’];

}

/* Add a Label */

echo ‘<li>’.$v[‘service_name’].’</li>’;

}

/* Close that Last Service */

echo ‘</ul>’;

And it worked perfectly.

If you have tips on how to improve this then please tell me :)



Comments

Leave a Reply

Your email address will not be published. Required fields are marked *