Display ACF repeater fields as Short Code.

Home » Snippets » Display ACF repeater fields as Short Code.
0

Created with:

Visibility: 

public

Creator: Vikas Gautam

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
// wp-plugin.php

// Define the shortcode function
function wpturbo_display_acf_repeater_shortcode($atts) {
    // Extract shortcode attributes
    extract(shortcode_atts(array(
        'field_name' => '', // Replace 'field_name' with the actual ACF repeater field name
    ), $atts));

    // Check if ACF exists
    if (function_exists('get_field')) {
        // Get the repeater field data
        $repeater_data = get_field($field_name);

        // Check if the repeater field has data
        if ($repeater_data) {
            // Initialize an output variable
            $output = '<ul>';

            // Loop through the repeater field data
            foreach ($repeater_data as $item) {
                // Get the subfield values
                $subfield1 = $item['subfield1']; // Replace 'subfield1' with the actual subfield name
                $subfield2 = $item['subfield2']; // Replace 'subfield2' with the actual subfield name

                // Add the subfield values to the output as list items
                $output .= '<li>Subfield 1: ' . $subfield1 . '</li>';
                $output .= '<li>Subfield 2: ' . $subfield2 . '</li>';
            }

            // Close the unordered list
            $output .= '</ul>';

            // Return the output
            return $output;
        }
    }

    // Return empty if ACF or the repeater field is not found
    return '';
}
add_shortcode('acf_repeater', 'wpturbo_display_acf_repeater_shortcode');
				

Explanation:

The customized code is similar to the previous code, but with a slight modification to display the subfield values as a bullet list. Here's the explanation:

With this modification, the subfield values will be displayed as a bullet list when using the shortcode [acf_repeater field_name="your_repeater_field_name"]. Just replace your_repeater_field_name with the actual name of your ACF repeater field.

Register an account to save your snippets or go Pro to get more features.