It might be handy to use the repeater custom field of Advanced Custom Fields plugin but it doesn’t work out of the box. So I created an example to give an idea of its usage. This particular shortcode uses a button from Impreza theme to show a set of buttons based on the repeater field:
if ( ! function_exists( 'ch_repeater_shortcode' ) ) {
/**
* Repeater Custom Field handler
* use [repeater-field] shortcode to call it from content or Grid Layout
*
* It has the next attributes style, repeater, title, link,
* where the three latest are custom field names
*/
add_shortcode( 'repeater-field', 'ch_repeater_shortcode' );
function ch_repeater_shortcode( $atts ) {
if ( is_admin() AND ! wp_doing_ajax() ) {
return FALSE;
}
// Parse attributes and apply default values
$atts = shortcode_atts(
array(
'type' => 'btn',
'style' => '1',
'repeater' => 'page_repeater',
'title' => 'page_repeater_text',
'link' => 'page_repeater_url',
), $atts
);
// Find repeater field
$repeater = get_field( esc_attr( trim( $atts['repeater'] ) ) );
if ( ! $repeater ) {
return FALSE;
}
$output = '';
if ( $atts['type'] == 'btn' ) {
foreach ( $repeater as $field ) {
$link = isset( $field[ $atts['link'] ] ) ? esc_url( $field[ $atts['link'] ] ) : NULL;
if ( $link ) {
$title = isset( $field[ $atts['title'] ] ) ? esc_attr( $field[ $atts['title'] ] ) : '';
$output .= '<div class="w-btn-wrapper align_none"><a class="w-btn us-btn-style_' . (int) $atts['style'] . '" href="' . $link . '"><span class="w-btn-label">' . $title . '</span></a></div>';
}
}
}
return $output;
}
}