Title: SCF repeater usage in function to create shortcode
Last modified: July 15, 2025

---

# SCF repeater usage in function to create shortcode

 *  Resolved [GEges](https://wordpress.org/support/users/georgeslevy/)
 * (@georgeslevy)
 * [11 months ago](https://wordpress.org/support/topic/scf-repeater-usage-in-function-to-create-shortcode/)
 * Hello guys. I switched from ACF to SCF because I wanted to use the repeater field.
   I’ve never used it before so I don’t know how to access the fields and sub-fields
   in a function to create a short code.
   I want if someone have already experienced
   it, and made it work, please consider sharing the code with us. I’ve been struggling
   with it for a long time now. Please I need help.

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [beegoodb](https://wordpress.org/support/users/beegoodb/)
 * (@beegoodb)
 * [11 months ago](https://wordpress.org/support/topic/scf-repeater-usage-in-function-to-create-shortcode/#post-18556418)
 * Hey! If I understood you correctly you want to output the sub fields. Here would
   be the function to do so.
 * Example: outputting navigation menu links.
 *     ```wp-block-code
       <?phpif (have_rows('site_menu')) :    while (have_rows('site_menu')) :        the_row(); ?>        <li>            <a href="<?= esc_url(get_sub_field('site_menu_url')) ?>">                <?= esc_html(get_sub_field('site_menu_title')) ?>            </a>        </li>    <?php endwhile;endif;?>
       ```
   
    -  This reply was modified 11 months ago by [beegoodb](https://wordpress.org/support/users/beegoodb/).
 *  [Yan Metelitsa](https://wordpress.org/support/users/yanmetelitsa/)
 * (@yanmetelitsa)
 * [11 months ago](https://wordpress.org/support/topic/scf-repeater-usage-in-function-to-create-shortcode/#post-18556510)
 * Hi!
 * **Example Repeater Field**
 * Let’s assume that we have created a Repeater field with the name `people`, which
   contains two text and number fields: `name` and `age`.
 * ![](https://i0.wp.com/i.postimg.cc/J0xD2kxX/image.png?ssl=1)
 * Because the Repeater field returns an array of data in the format:
 *     ```wp-block-code
       [	0 => [		'name' => '...',		'age'  => ...,	],	1 => [		'name' => '...',		'age'  => ...,	],	...]
       ```
   
 * it will not be possible to use the regular ACF shortcode, so you will have to
   create a custom shortcode.
 * **Universal Repeater Shortcode**
 * Open your `functions.php` file and use this code snippet:
 *     ```wp-block-code
       add_shortcode( 'acf_repeater', function ( $atts, $content = '' ) {	$atts = shortcode_atts([		'field'      => '',		'sub_fields' => '',		'post_id'    => null,	], $atts );	if ( empty( $atts['field'] ) || empty( $atts['sub_fields'] ) ) return '';	$sub_fields = array_map( 'trim', explode( ',', $atts['sub_fields'] ) );		$output = '';	if ( have_rows( $atts['field'], $atts['post_id'] ) ) {		while ( have_rows( $atts['field'], $atts['post_id'] ) ) {			the_row();			$row = $content;			foreach ( $sub_fields as $sub ) {				$value = get_sub_field( $sub );				$row   = str_replace( "%{$sub}%", $value, $row );			}			$output .= do_shortcode( $row );		}	}	return $output;});
       ```
   
 * In my examples I didn’t use escape functions (`esc_html()`, `esc_url()`, etc.)
   to save space, but following [beegoodb](https://wordpress.org/support/users/beegoodb/)‘
   s example, be sure to use them!
 * This function creates a universal shortcode for use in your theme:
 *     ```wp-block-code
       [acf_repeater field="people" sub_fields="name,age"]	<div class="person">		<strong>%name%</strong><br>		<em>%age% yo</em>	</div>[/acf_repeater]
       ```
   
 * **Specialized Shortcode**
 * You can also create a shortcode specifically for displaying data about people
   in this case:
 *     ```wp-block-code
       add_shortcode( 'acf_people', function ( $atts, $content = '' ) {	$atts = shortcode_atts([		'post_id' => null,	], $atts );		$output = '';	if ( have_rows( 'people', $atts['post_id'] ) ) {		while ( have_rows( 'people', $atts['post_id'] ) ) {			the_row();			$output = 'Name: ' . get_sub_field( 'name' ) . ', age: ' . get_sub_field('age');		}	}	return $output;});
       ```
   
 * In this case, just use: `[acf_people]`
 * **PHP Usage**
 * But I recommend working with the Repeater field directly through the PHP:
 *     ```wp-block-code
       foreach ( get_field( 'people' ) as $people ) {	echo 'Name: ' . $people['name'] . ', age:' . $people['age'];}
       ```
   
 *  Thread Starter [GEges](https://wordpress.org/support/users/georgeslevy/)
 * (@georgeslevy)
 * [10 months, 4 weeks ago](https://wordpress.org/support/topic/scf-repeater-usage-in-function-to-create-shortcode/#post-18558683)
 * This is how I made it work:
 * function past_montante_shortcode() {
   ob_start(); // Start output buffering
 *     ```wp-block-code
       // Query all the prono posts
       $args = array(
           'post_type' => 'pronostic',
           'posts_per_page' => 1, // Fixed typo (was post_per_page)
           'post_status' => 'publish' // Fixed (was published)
       );
   
       $prono_query = new WP_Query($args);
   
       if($prono_query->have_posts()) : 
           while($prono_query->have_posts()) : $prono_query->the_post();
               $montantes_precedentes = get_field('montantes_precedentes', get_the_ID());
   
               if(!empty($montantes_precedentes)) : ?>
                   <section id="past-montantes-splide-section" class="splide" aria-label="Past Montantes Slider">
                       <div class="splide__track">
                           <ul class="splide__list">
                               <?php foreach($montantes_precedentes as $montante) : 
                                   if(!empty($montante['image_montante'])) : ?>
                                       <li class="splide__slide">
                                           <img src="<?php echo esc_url($montante['image_montante']); ?>" 
                                                alt="Montante précédente" 
                                                class="splide-slide-image"
                                                loading="lazy">
                                       </li>
                                   <?php endif;
                               endforeach; ?>
                           </ul>
                       </div>
                   </section>
   
                   <script>
                       document.addEventListener('DOMContentLoaded', function() {
                           if(typeof Splide !== 'undefined') {
                               const pastMontanteDesktopSlider = new Splide('.splide', {
                                   type: 'loop',
                                   perPage: 1,
                                   perMove: 1,
                                   gap: '1rem',
                                   pagination: true,
                                   arrows: true,
                                   lazyLoad: 'nearby',
                                   breakpoints: {
                                       768: { perPage: 1 }
                                   }
                               }).mount();
                           }
                       });
                   </script>
               <?php else : ?>
                   <p class="no-montantes">No montantes précédentes found.</p>
               <?php endif;
           endwhile;
       endif;
   
       wp_reset_postdata();
       return ob_get_clean();
       ```
   
 * }
   add_shortcode(‘past_montantes’, ‘past_montante_shortcode’);
 * it works just fine
 * Thank you all for your responses and your help.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘SCF repeater usage in function to create shortcode’ is closed to new 
replies.

 * ![](https://ps.w.org/secure-custom-fields/assets/icon.svg?rev=3194494)
 * [Secure Custom Fields](https://wordpress.org/plugins/secure-custom-fields/)
 * [Support Threads](https://wordpress.org/support/plugin/secure-custom-fields/)
 * [Active Topics](https://wordpress.org/support/plugin/secure-custom-fields/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/secure-custom-fields/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/secure-custom-fields/reviews/)

## Tags

 * [repeater](https://wordpress.org/support/topic-tag/repeater/)
 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)

 * 3 replies
 * 3 participants
 * Last reply from: [GEges](https://wordpress.org/support/users/georgeslevy/)
 * Last activity: [10 months, 4 weeks ago](https://wordpress.org/support/topic/scf-repeater-usage-in-function-to-create-shortcode/#post-18558683)
 * Status: resolved