Home › Forums › General Issues › help with checkbox and repeater in custom template
Hello forum! I am new with ACF and php in general so struggling a bit. I am trying to display a list of supplementary info in a sidebar which either link to pages within my site, or link to a PDF Download.
I would like the user in the backend to be able to input the info like this: In row or column 1—input the title of the link;
In row or column 2—choose type of link using checkbox or similar; (Choices page : Page Link and file : File Download);
In row or column 3—Using conditional Logic IF Page Link is checked: choose page link (use drop down to select page) or IF File Download is checked choose the file to upload to site.
I think I did that part ok using repeater add-on (will upload screen shot of general set up) but my problem is displaying the content on the front end. I can get a list of link titles to appear, but I can’t figure out how to filter the link itself (to another page or to a linked file). Im trying conditional below but not seem to be getting anywhere.
<aside>
<?php if ( in_category( ‘farrell’ )) {
$rows = get_field(‘reimburs_links’);
if($rows)
{
echo ‘<h2>Reimbursement Information</h2>’;
echo ‘<ul>’;
foreach($rows as $row)
{
if(get_sub_field(‘type_of_link’) == “page”)
{
echo ‘<li><a href=”’.$row[‘page_link’].’”>’.$row[‘link_title’].’</a></li>’;
}
if(get_sub_field(‘type_of_link’) == “file”)
{
echo ‘<li><a href=”’.$row[‘file_link’].’”>’.$row[‘link_title’].’</a></li>’;
}
}
echo ‘</ul>’;
}
} ?>
</aside>
Ive spent hours on this reading and looking for tutorials. Tried ACF docs (hard to absorb at my level), googling and youtube so if anyone could please help… THANKS!
Hey @eileen.schmidtke.
You’re on the right track there with the logic, you’re just missing a key function to make the check boxes work as conditional options.
You can find the documentation here: http://www.advancedcustomfields.com/resources/checkbox/
The part you should look at is this:
/*
* Conditional statement (Checkbox rvalue is an array)
*/
if( in_array( 'red', get_field('field_name') ) )
{
//...
}
So for your code would look like this … I’ve cleaned it up and commented it a bit as well 🙂
<aside>
<?php if ( in_category( 'farrell' )) {
$rows = get_field('reimburs_links');
if($rows) {
echo '<h2>Reimbursement Information</h2>';
echo '<ul>';
foreach($rows as $row) {
// If Page is checked, use page link and title
if(in_array('page', $row['type_of_link'])) {
echo '<li><a href=”'.$row['page_link'].'”>'.$row['link_title'].'</a></li>';
}
// If file is checked, use file link and title
if(in_array('file', $row['type_of_link'])) {
echo '<li><a href=”'.$row['file_link'].'”>'.$row['link_title'].'</a></li>';
}
}
echo '</ul>';
}
} ?>
</aside>
Hope that helps.
The topic ‘help with checkbox and repeater in custom template’ 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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.