Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.