Home › Forums › General Issues › Conditional logic to change output depending on which fields are active › Reply To: Conditional logic to change output depending on which fields are active
Below is the test code I was playing with. I used variables rather than ACF fields, but you can swap them back out.
Also… I used || for OR, && for AND, and ! for “not equal to”
Also… in the last conditional where you check for “One single link”… that won’t trigger when it’s just link 3, because the condition is already met when you output “Link 1 and Link 2 OR Link 3”.
If you would like to tell me your purpose for the code, I may be able to offer a better solution.
<?php
$link1 = '1';
$link2 = '';
$link3 = '';
if ( $link1 && $link2 && $link3 ) {
echo '<div class="red">';
echo '<p>All 3 Links</p>';
echo '</div>';
} else if ( ( $link1 && $link2 ) || ( $link3 && !$link1 && !$link2 ) ) {
echo '<div class="blue">';
echo '<p>Link 1 and Link 2 OR Link 3</p>';
echo '</div>';
} else if ( $link2 && $link3 && !$link1 ) {
echo '<div class="green">';
echo '<p>Link 2 and Link 3 But not Link 1</p>';
echo '</div>';
} else if ( ( $link3 && !$link1 && !$link2 ) || ( !$link3 && !$link1 && $link2 ) || ( !$link3 && $link1 && !$link2 ) ) {
echo '<div class="hotpink">';
echo '<p>One single Link</p>';
echo '</div>';
}
?>
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.