Home › Forums › Front-end Issues › Conditional check of true/false field on another page – Advanced Custom Fields
I have a repeater field with a true/false field marking one of the subfields as the head office of a business.
I’ve create my WP pages like this.
Home
About
Contact
> Offices
I’ve set up my field group with repeater and told it to use this “Offices” page.
What I now need to do is cycle through all the offices and check which one is the head office so that I can show that office and style it slightly differently from the all the other offices.
I’m dumping my variable out like this: var_dump( get_field('is_head_office' , 382) );
since it is content from another page I want to show on the parent page (Contact).
This returns bool(false)
Pseudo coding this I would do the following:
H2 Head Office H2
if true/false field from page 382
show the head office
H2 Other Offices H2
if true/false fields from page 382 is false
show all other offices
Now I just need to correct syntax. This is working to get all the fields.
<?php while(has_sub_field('office', 382)): ?>
<?php the_sub_field('city'); ?>
<?php the_sub_field('address'); ?>
<?php the_sub_field('telephone'); ?>
<?php endwhile; ?>
Thanks!
I’ve tried now to set each other sub fields to true and ran the code again but all of them return false which tells me something else must be wrong here?
The checkbox field ‘is_head_office’ is a sub field of the ‘office’ repeater, correct?
If so, you can’t access it’s value with var_dump( get_field('is_head_office' , 382) );
as you must be within a loop to see the current row’s value.
I would code it like so:
<h2>Head Office</h2>
<?php while(has_sub_field('office', 382)): ?>
<?php if( get_sub_field('is_head_office')): ?>
<?php the_sub_field('city'); ?>
<?php the_sub_field('address'); ?>
<?php the_sub_field('telephone'); ?>
<?php endif; ?>
<?php endwhile; ?>
<h2>Other Offices</h2>
<?php while(has_sub_field('office', 382)): ?>
<?php if( !get_sub_field('is_head_office')): ?>
<?php the_sub_field('city'); ?>
<?php the_sub_field('address'); ?>
<?php the_sub_field('telephone'); ?>
<?php endif; ?>
<?php endwhile; ?>
Hope that helps.
Thanks
E
Thanks Elliot that works like a charm and I understand the logic behind it now.
Many thanks.
Can you mark yours as the answer instead of mine 😉
The topic ‘Conditional check of true/false field on another page – Advanced Custom Fields’ 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.