display ACF repeater fields with a shortcode

193 Views Asked by At

I have ACF pro and Divi, and I struggle to display a field that is a repeater. So I tried to do function to add in function.php file to display my fields through a shortcode.

I don't know much about php, I'm sorry, but I tried to get some infos to build my own function.

Can you help me for that?

My repeater has 7 sub-fields and the result of one row is like: Paris, April 11th at 11h30 Marseille, from April 11th to April 13th, at 10:00

So the editor can add a town, with a start date, and an end date if there is one, then an hour. the texts 'from', 'to' and 'at' are displayed under condition (end date not empty for example).

This is my code, but I get a critical error from my wordpress.

// ########### récupérer ACF repeater lieu_date_heure
function get_lieu_date_heure()
{
  if (have_rows('lieu_date_heure')): /* this is repeater field */
    while (have_rows('lieu_date_heure')):
        the_row();
        $ville = the_sub_field('ville'); /* this is repeater subfield */
        $du = the_sub_field('du'); /* this is repeater subfield */
        $date_debut = the_sub_field('date_debut'); /* this is repeater subfield */
        $au = the_sub_field('au'); /* this is repeater subfield */
        $date_fin = the_sub_field('date_fin'); /* this is repeater subfield */
        $a = the_sub_field('a'); /* this is repeater subfield */
        $heure = the_sub_field('heure'); /* this is repeater subfield */
   endwhile;
    echo '<div class="lieu_date_heure"><p>' . $ville . ', ' . $du. ' ' . $date_debut . ' au ' . $date_fin . ' à ' . $heure</p></div>';
  return ob_get_clean();
}

add_shortcode('show_lieu_date_heure', 'get_lieu_date_heure');

Thanks Caroline

1

There are 1 best solutions below

1
On

I tried this one. I have no more critical error, but no content neither.

function get_lieu_date_heure()
{
  ob_start(); // Initialiser le tampon de sortie

  if (have_rows('lieu_date_heure')): /* this is repeater field */
    while (have_rows('lieu_date_heure')):
        the_row();
        $ville = the_sub_field('ville'); /* this is repeater subfield */
        $du = the_sub_field('du'); /* this is repeater subfield */
        $date_debut = the_sub_field('date_debut'); /* this is repeater subfield */
        $au = the_sub_field('au'); /* this is repeater subfield */
        $date_fin = the_sub_field('date_fin'); /* this is repeater subfield */
        $a = the_sub_field('a'); /* this is repeater subfield */
        $heure = the_sub_field('heure'); /* this is repeater subfield */
        echo '<div class="lieu_date_heure"><p>' . $ville . ', ' . $du . $date_debut . ' ' . $au . $date_fin . ' ' . $a . $heure . '</p></div>';
    endwhile;
  endif;

  return ob_get_clean(); // Récupérer et nettoyer la sortie du tampon
}

add_shortcode('show_lieu_date_heure', 'get_lieu_date_heure');

I also tris with get_sub_field instead of the_sub_field and it's the same: no content displays

Caroline