Show an ACF Repeater as a List
Adds an [acf_list name="features" sub="text"] shortcode that turns a repeater field into a bulleted list, with an optional second sub field shown in bold.
Opens WPIDE on
Code
add_shortcode( 'acf_list', function ( $atts ) {
$atts = shortcode_atts( array( 'name' => '', 'sub' => '', 'label' => '', 'post_id' => 0 ), $atts, 'acf_list' );
$post_id = $atts['post_id'] ? absint( $atts['post_id'] ) : get_the_ID();
if ( ! $post_id || ! function_exists( 'get_field' ) || ! is_post_publicly_viewable( $post_id ) ) {
return '';
}
$rows = get_field( sanitize_key( $atts['name'] ), $post_id );
if ( ! is_array( $rows ) || ! $rows ) {
return '';
}
$sub = sanitize_key( $atts['sub'] );
$label = sanitize_key( $atts['label'] );
$items = '';
foreach ( $rows as $row ) {
if ( ! is_array( $row ) || ! isset( $row[ $sub ] ) || ! is_scalar( $row[ $sub ] ) ) {
continue;
}
$prefix = ( $label && isset( $row[ $label ] ) && is_scalar( $row[ $label ] ) ) ? '<strong>' . esc_html( $row[ $label ] ) . ':</strong> ' : '';
$items .= '<li>' . $prefix . esc_html( $row[ $sub ] ) . '</li>';
}
return $items ? '<ul class="acf-list acf-list-' . esc_attr( sanitize_html_class( $atts['name'] ) ) . '">' . $items . '</ul>' : '';
} );
Notes
Example: [acf_list name="specs" label="name" sub="value"]. Repeaters need ACF PRO.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.