ACF: Display Repeater Rows via Shortcode
Registers a [scseed_acf_repeater field="my_repeater" sub="item"] shortcode that iterates a repeater field and outputs its rows as a list.
PHPby SnipCraft
php
<?php
add_shortcode( 'scseed_acf_repeater', function( $atts ) {
if ( ! function_exists( 'have_rows' ) ) {
return '';
}
$atts = shortcode_atts( [
'field' => '',
'post_id' => null,
'sub' => 'item',
], $atts, 'scseed_acf_repeater' );
if ( empty( $atts['field'] ) ) {
return '';
}
$field_name = sanitize_key( $atts['field'] );
$sub_name = sanitize_key( $atts['sub'] );
$post_id = ! empty( $atts['post_id'] ) ? absint( $atts['post_id'] ) : null;
if ( ! have_rows( $field_name, $post_id ) ) {
return '';
}
$output = '<ul class="scseed-repeater-list">';
while ( have_rows( $field_name, $post_id ) ) {
the_row();
$sub_value = get_sub_field( $sub_name );
if ( $sub_value !== false && $sub_value !== null && $sub_value !== '' ) {
$output .= '<li>' . wp_kses_post( (string) $sub_value ) . '</li>';
}
}
$output .= '</ul>';
return $output;
} );#acf#custom-fields#frontend#repeater#shortcode