Home › Forums › General Issues › Loop to generate fields group shorcode ACF Free
Hi everyone,
I have a problem to generate dynamicaly sub_fields or field group. I using actually ACF Free.
I used shortcode to display fields:
shorcode.php
function my_shortcode($attrs) {
$attrs = shortcode_atts( [
'post_id' => '', // Default value.
], $attrs );
//récupérer le lien
$linkOutput = '[acf field="cours_1_lien_de_la_page" post_id="' . $attrs['post_id'] . '"]';
$linkOutput = do_shortcode( $linkOutput );
//récupérer l'image
$imgOutput = '[acf field="cours_1_image" post_id="' . $attrs['post_id'] . '"]';
$imgOutput = do_shortcode( $imgOutput );
$imgOutput = 'background-image:url('.$imgOutput.')';
//récupérer le titre
$titleOutput = '[acf field="cours_1_titre" post_id="' . $attrs['post_id'] . '"]';
$titleOutput = do_shortcode( $titleOutput );
//récupérer le lien
$linkOutput2 = '[acf field="cours_2_lien_de_la_page" post_id="' . $attrs['post_id'] . '"]';
$linkOutput2 = do_shortcode( $linkOutput2 );
//récupérer l'image
$imgOutput2 = '[acf field="cours_2_image" post_id="' . $attrs['post_id'] . '"]';
$imgOutput2 = do_shortcode( $imgOutput2 );
$imgOutput2 = 'background-image:url('.$imgOutput2.')';
//récupérer le titre
$titleOutput2 = '[acf field="cours_2_titre" post_id="' . $attrs['post_id'] . '"]';
$titleOutput2 = do_shortcode( $titleOutput2 );
//récupérer le lien
$linkOutput3 = '[acf field="cours_3_lien_de_la_page" post_id="' . $attrs['post_id'] . '"]';
$linkOutput3 = do_shortcode( $linkOutput3 );
//récupérer l'image
$imgOutput3 = '[acf field="cours_3_image" post_id="' . $attrs['post_id'] . '"]';
$imgOutput3 = do_shortcode( $imgOutput3 );
$imgOutput3 = 'background-image:url('.$imgOutput3.')';
//récupérer le titre
$titleOutput3 = '[acf field="cours_3_titre" post_id="' . $attrs['post_id'] . '"]';
$titleOutput3 = do_shortcode( $titleOutput3 );
//récupérer le lien
$linkOutput4 = '[acf field="cours_4_lien_de_la_page" post_id="' . $attrs['post_id'] . '"]';
$linkOutput4 = do_shortcode( $linkOutput4 );
//récupérer l'image
$imgOutput4 = '[acf field="cours_4_image" post_id="' . $attrs['post_id'] . '"]';
$imgOutput4 = do_shortcode( $imgOutput4 );
$imgOutput4 = 'background-image:url('.$imgOutput4.')';
//récupérer le titre
$titleOutput4 = '[acf field="cours_4_titre" post_id="' . $attrs['post_id'] . '"]';
$titleOutput4 = do_shortcode( $titleOutput4 );
//récupérer le lien
$linkOutput5 = '[acf field="cours_5_lien_de_la_page" post_id="' . $attrs['post_id'] . '"]';
$linkOutput5 = do_shortcode( $linkOutput5 );
//récupérer l'image
$imgOutput5 = '[acf field="cours_5_image" post_id="' . $attrs['post_id'] . '"]';
$imgOutput5 = do_shortcode( $imgOutput5 );
$imgOutput5 = 'background-image:url('.$imgOutput5.')';
//récupérer le titre
$titleOutput5 = '[acf field="cours_5_titre" post_id="' . $attrs['post_id'] . '"]';
$titleOutput5 = do_shortcode( $titleOutput5 );
//construction du html
$par = '<section class="courses-sc">';
$par .= '<div class="courses-sc__list">';
$par .= '<a href="'.$linkOutput.'">';
$par .= "<div class='courses-sc__card card shadow' style=".$imgOutput.">";
$par .= '<h3 class="courses-sc__card-heading heading heading--h3">'.$titleOutput.'</h3>';
$par .= '</div>';
$par .= '</a>';
$par .= '<a href="'.$linkOutput2.'">';
$par .= "<div class='courses-sc__card card shadow' style=".$imgOutput2.">";
$par .= '<h3 class="courses-sc__card-heading heading heading--h3">'.$titleOutput2.'</h3>';
$par .= '</div>';
$par .= '</a>';
$par .= '<a href="'.$linkOutput3.'">';
$par .= "<div class='courses-sc__card card shadow' style=".$imgOutput3.">";
$par .= '<h3 class="courses-sc__card-heading heading heading--h3">'.$titleOutput3.'</h3>';
$par .= '</div>';
$par .= '</a>';
$par .= '<a href="'.$linkOutput4.'">';
$par .= "<div class='courses-sc__card card shadow' style=".$imgOutput4.">";
$par .= '<h3 class="courses-sc__card-heading heading heading--h3">'.$titleOutput4.'</h3>';
$par .= '</div>';
$par .= '</a>';
$par .= '<a href="'.$linkOutput5.'">';
$par .= "<div class='courses-sc__card card shadow' style=".$imgOutput5.">";
$par .= '<h3 class="courses-sc__card-heading heading heading--h3">'.$titleOutput5.'</h3>';
$par .= '</div>';
$par .= '</a>';
$par .= '</div>';
$par .= '</section>';
return $par;
}
// cette fonction prend deux arguments = le nom du shortcode en lui meme, le deuxieme, le nom de la fonction
add_shortcode( 'listeDesCours', 'my_shortcode' );
Can you help me to generate differents subfields dynamically ?
Thank you by advance.
See you soon
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.