Home › Forums › Add-ons › Repeater Field › Troubles with true/false in repeater
Hello ACF Forum members,
Ive been fiddling with some code for quite some time and Im wondering if someone could help me see what I am doing wrong.
I am using the repeater field to display a list of links in the sidebar. I had it working just fine using a foreach loop but now I have added an additional true/false link within the repeater so users can check a box to hide the link if it is not executable on a phone or tablet. SO basically I want to inject a css class to the li tag to hide the link title from view on all devices except desktop. This is what I have so far:
<aside class="widget">
<?php
$rows = get_field('tool_section_links');
if($rows)
{
echo '<h2>Tools</h2>';
echo '<ul>';
foreach($rows as $row)
{
if (get_sub_field ('show_desktop') == true ) {
echo '<li class="show-for-large-up"><a href="'.$row['tool_link'].'">'.$row['tool_link_title'].'</a></li>';
}
else {
echo '<li><a href="'.$row['tool_link'].'">'.$row['tool_link_title'].'</a></li>';
}
}
echo '</ul>';
}
?>
</aside>
Ive spent a lot of time looking for examples of true/false usage in the repeater but not much that I could find was much help. It seems like it should be quite basic so I must be missing something simple? Do I need to structure this differently without the foreach loop? thanks in advance.
Eileen
i dont know if true/false has Problem with repeater.
try if it works when you dont use == true, to ckeck if checked like that:
if( get_sub_field ('show_desktop') ) {
...
} else {
...
}
if this also dont work, you could try to replace true/false with a
Radio Button or Check-Box field with value “yes” (and “no” when you use radio-button) and use code like that.
$show_desktop = get_sub_field ('show_desktop');
if ($show_desktop == 'yes'){
...
} else {
...
}
of course you can replace yes/no with any value you wish when you use radio-button/check-box, just adapt code above
Thanks Mediawerk. I had originally tried the checkbox which did not work so I tried true/false. I just tried the radio button but that has no affect either. I can’t get the class in the li tag to appear. Any other suggestions?
could you try this (use have_rows and a wile loop instead of your foreach loop) :
<aside class="widget">
<?php
if( have_rows('tool_section_links') ):
echo '<h2>Tools</h2>';
echo '<ul>';
while( have_rows('tool_section_links') ): the_row();
$show_desktop = get_sub_field('show_desktop');
$tool_link = get_sub_field('tool_link');
$tool_link_title = get_sub_field('tool_link_title');
if ($show_desktop == 'yes'){
echo '<li class="show-for-large-up"><a href="'.$tool_link.'">'.$tool_link_title.'</a></li>';
} else {
echo '<li><a href="'.$tool_link.'">'.$tool_link_title.'</a></li>';
}
endwhile;
echo '</ul>';
endif;
?>
</aside>
depend on witch fieldtype you use change if ($show_desktop == 'yes')
to correct value
That worked! Those types of fields don’t seem to work well on a foreach loop. Restructuring the loop was my next plan of attack. Appreciate you sticking with this. Thanks Mediawerk, have a great day!
I Try to manage someting similar, but the sub_field “zusatzbutton” doesn’t work.
Any thought?
<?php
$seite = get_field('seite');
$zusatzbutton = get_sub_field('zusatzbutton-check');
if($seite) {
echo '<div class="seiten grid-3 wrapper">';
foreach($seite as $s) {
echo '
<div class="seite column">
<div class="seite-bild"><a href="'.$s['button-link'].'"><img src="'.$s['seite-bild'].'"></a></div>
<h2>'.$s['seite-text'].'</h2>
<a class="weiterlesen" href="'.$s['button-link'].'">'.$s['button-text'].'</a>';
if($zusatzbutton == 'yes') {
echo '<a class="weiterlesen" href="'.$s['zusatzbutton-link'].'">'.$s['zusatzbutton-text'].'</a>';
}
echo '</div>';
}
echo '</div>';
}
?>
The topic ‘Troubles with true/false in repeater’ 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.