Home › Forums › Add-ons › Repeater Field › Loop Repeater Based on Sub Field Values
I have a Repeater Field called: package_name
In it i have a Select Sub Field called:package_group
In Select Sub Field i have the option:caci_facials : Caci Facials.
I am trying to get all package_name with Select Sub Field “Caci Facials” (or is it ‘Caci Facials’ or caci_facial’ or ‘caci facials’) to be listed on a different page.
Normally when i have to show Advanced Custom Fields on other pages i have to add ‘option’ into $rows = get_field(‘my_field’, ‘option’); for it to work.
ALSO, its all wrapped up in a shortcode.
I have tried the code on another page in this forum, changing this:
if(have_rows('field_name')):
while(have_rows('field_name')): the_row();
if(get_sub_field('sub_field') == 'functional assay'):
the_sub_field('sub_field');
endif;
endwhile;
endif;
to
// PackageList shortcode
function treatmentlistf($atts, $content = null) { ?>
<?php
if(have_rows('package_name', 'option')):
while(have_rows('package_name', 'option')): the_row();
if(get_sub_field('package_group') == 'caci_facials'): ?>
<h2> Test hello</h2>
<?php endif;
endwhile;
endif;
?>
<?php }
add_shortcode('treatmentlist', 'treatmentlistf');
Nothing happens – rest of page shows but not shortcode content.
What am i doing wrong
Right, couldn’t come up with a solution to this so gave up and added them manually instead.
Hi @tomwgf
You need to return the shortcode instead of printing it like that. It should be something like this:
// PackageList shortcode
function treatmentlistf($atts, $content = null) { ?>
<?php
if(have_rows('package_name', 'option')):
while(have_rows('package_name', 'option')): the_row();
if(get_sub_field('package_group') == 'caci_facials'): ?>
return '<h2> Test hello</h2>'
<?php endif;
endwhile;
endif;
?>
<?php }
add_shortcode('treatmentlist', 'treatmentlistf');
This page should give you more idea about it: https://codex.wordpress.org/Function_Reference/add_shortcode.
Hope this helps.
hi james
thanks for replying.
i tried your code – but it didn’t work.
i also tried changing return ‘<h2> Test hello</h2>’ to <?php return ‘<h2> Test hello</h2>’; ?>
is ‘option’ right, is == ‘caci_facials’ right??
tom
Hi @tomwgf
Sorry about that. I forgot to remove the php tag. It should be working with this one:
// PackageList shortcode
function treatmentlistf($atts, $content = null) {
$returned = "";
if(have_rows('package_name', 'option')):
while(have_rows('package_name', 'option')): the_row();
if(get_sub_field('package_group') == 'caci_facials'):
$returned .= "<h2>Test Hello</h2>";
endif;
endwhile;
endif;
return $returned;
}
add_shortcode('treatmentlist', 'treatmentlistf');
I hope this helps.
hi james
thanks, but still can’t get it to work. do you think the 2 , ‘option’s are helping?? i’ve tried without – no joy
tom
Hi @tomwgf
That’s weird. Could you please try to remove the $content = null so it looks like this?
// PackageList shortcode
function treatmentlistf($atts) {
$returned = "";
if(have_rows('package_name', 'option')):
while(have_rows('package_name', 'option')): the_row();
if(get_sub_field('package_group') == 'caci_facials'):
$returned .= "<h2>Test Hello</h2>";
endif;
endwhile;
endif;
return $returned;
}
add_shortcode('treatmentlist', 'treatmentlistf');
The “option”s are used if you put the field group on an options page. If you put it on a post/page, I believe you don’t need it. You can also try to set a value for the $returned variable so it looks like this:
$returned = "the value";
If it the shortcode only returns “the value”, it means there is something wrong with the field.
I hope this makes sense.
Saw this post and it helped me with my shortcode for a repeater field, so I thought I’d post the code I used:
// Box Navigation shortcode
function boxnavf($atts) {
ob_start();
echo '<div class="box-navigation" > ';
if( get_field('box-navigation') ):
echo ' <ul class="boxes">';
while( has_sub_field('box-navigation') ):
echo '<li class="item">';
echo '<a href='.get_sub_field("box-link").'>' .get_sub_field("box-page-name"). '</a>';
echo '</li> ';
endwhile;
echo '</ul/>';
endif;
echo '</div>';
$output = ob_get_clean();
return $output;
}
add_shortcode('boxnav', 'boxnavf');
Thanks
Dan
Hi James
sorry for not replying before – very rude when you were helping
cant remember what happened
i think i did it all manually in the end
tom
The topic ‘Loop Repeater Based on Sub Field Values’ is closed to new replies.
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.