Home › Forums › Add-ons › Repeater Field › loop in function.php problem
Hello
I’m making a shortcode, I have a problem in my code but I don’t know what it’s wrong.
function categorie_shortcode_callback() {
$html = '';
$categories = get_categories( array(
'taxonomy' => 'carte-category',
'parent' => 0
));
shuffle( $categories);
if ( ! empty($categories) ) {
$category = $categories[0];
$html.= '<h3> '. $category->name. '</h3>';
$html.= '<p class="txtContent"> '. $category->description. '</p>';
$random_posts = get_posts( array(
'post_type' => 'carte',
'posts_per_page' => 1,
'orderby' => 'rand',
'fields' => 'ids',
'tax_query' => array(
array( 'taxonomy' => 'carte-category', 'terms' => $category->term_id, 'field' => 'term_id', 'category__not_in' =>[ 8, 9, 10] )
)
) );
if ( ! empty($random_posts) ) {
$html.='<h4>'.get_the_title( $random_posts[0]).'</h4>';
if ($category->term_id = 5){
while ( have_rows('formule', $random_posts[0]) ) : the_row();
while ( have_rows('tarif', $random_posts[0]) ) : the_row();
$html.= '<span class="type">'.get_sub_field('type', $random_posts[0]).'</span>';
$html.= '<span class="prix">'.get_sub_field('tarif', $random_posts[0]).' € </span>';
endwhile;
break;
endwhile;
$html .= get_the_post_thumbnail( $random_posts[0]);
} else{
$produits = get_field('produits', $random_posts[0]);
foreach($produits as $produit){
$nom = $produit['nom_du_produit'];
$html.= '<span class="type">'.$nom.'</span>';
}
}
}
}
return $html;
}
add_shortcode( 'categorie', 'categorie_shortcode_callback' );
the part after “else” is not working, my fields name are good and the content exists.
Someone knows how to resolve it ?
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!
The most recent ACF Chat Friday featured a live demo of how to register CPTs directly in the plugin, one of our most requested features. Check out the summary below for a replay of the demo, and don’t forget to register for the next session! https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 9, 2023
© 2023 Advanced Custom Fields.
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.