0

I am using a foreach loop to look at custom taxonomies associated with my custom post type. The issue I am having is that I am getting multiple buttons to display for posts that have more than one tax term selected.

What I would like to have happen is the loop search for one or more of the "age/grades" and then display a single button with the correct text and link. However, I am getting one button for each grade selected for the program (e.g. a Grade 3-6 program has four buttons: one for each applicable grade).

Any idea on how to prevent the duplicates?

Here is my current code:

<?php 
$agegroup = wp_get_post_terms(get_the_ID(), 'camper_grade');
    if ($agegroup) {
        foreach ($agegroup as $group) {
            if ($group->slug == 'age-2' || 'age-3' || 'age-4') { ?>
                <a href="/preschool">
                <div class="blue-btn">
                    More Preschool Camps
                </div>
                </a> <?php ;
            }

            elseif ($group->slug == '1st-grade' || '2nd-grade' || '3rd-grade' || '4th-grade' || '5th- 
                    grade' || '6th-grade') { ?>
                <a href="/grades-k-6">
                <div class="blue-btn">
                    More Grade K-6 Camps
                </div>
                </a> <?php ;
            }

            elseif ($group->slug == '7th-grade' || '8th-grade' || '9th-grade' || '10th-grade' || '11th- 
                    grade' || '12th-grade' ) { ?>
                <a href="/teen">
                <div class="blue-btn">
                    More Teen Adventures
                </div>
                </a> <?php ;
            }
        }
    }
?>
Nick
  • 138,499
  • 22
  • 57
  • 95
Iisrael
  • 397
  • 4
  • 17
  • First of all, this comparison **if ($group->slug == 'age-2' || 'age-3' || 'age-4')** will evaluate **$group->slug == 'age-2'** and it's ok, but the next will always return true since 'age-3' and 'age-4' are not null nor empty. Try this: **if(in_array($group->slug, ['age-2', 'age-3', 'age-4']))** – Triby Dec 24 '19 at 03:08

2 Answers2

1

You need to add a flag to indicate whether the button has been displayed already for that group, and only output the button if it hasn't. Note that your logical conditions are incorrect, you need to compare the slug with each value individually (or better yet, use in_array). For example:

if ($agegroup) {
    $preschool = $grades_k_6 = $teen = false;
    foreach ($agegroup as $group) {
        if (in_array($group->slug, array('age-2', 'age-3', 'age-4')) && !$preschool) { ?>
            <a href="/preschool">
            <div class="blue-btn">
                More Preschool Camps
            </div>
            </a> <?php ;
            $preschool = true;
        }
        elseif (in_array($group->slug, array('1st-grade', '2nd-grade', '3rd-grade', '4th-grade', '5th-grade', '6th-grade')) && !$grades_k_6) { ?>
            <a href="/grades-k-6">
            <div class="blue-btn">
                More Grade K-6 Camps
            </div>
            </a> <?php ;
            $grades_k_6 = true;
        }
        elseif (in_array($group->slug, array('7th-grade', '8th-grade', '9th-grade', '10th-grade', '11th-grade', '12th-grade')) && !$teen) { ?>
            <a href="/teen">
            <div class="blue-btn">
                More Teen Adventures
            </div>
            </a> <?php ;
            $teen = true;
        }
    }
}
Nick
  • 138,499
  • 22
  • 57
  • 95
  • @Iisrael did you see this answer? It's a lot more efficient than the other (only one `foreach` loop instead of 3). – Nick Dec 24 '19 at 03:14
  • thank you for your answer. I will give that a try and an upvote, sorry I just started at the first one. – Iisrael Dec 24 '19 at 03:19
  • No worries - but don't upvote until you've tried it. You must have different ordering for posts as this was the first answer posted... 42 mins ago vs 36... – Nick Dec 24 '19 at 03:21
  • yea, looks like yours did come in first, but was on the bottom of the stack. In any case thank you as well. I set up everything with your code and I got the same desired result. So it looks like a six of one, half dozen of the other scenario – Iisrael Dec 24 '19 at 03:57
  • I'm glad it's working for you - it's always good to have multiple possibilities. – Nick Dec 24 '19 at 03:59
1

Use separate foreach loops and break when it's found.

Your || is also not working how you want it to. You should use in_array which correctly compares the same value to many others:

<?php 
$agegroup = wp_get_post_terms(get_the_ID(), 'camper_grade');
    if ($agegroup) {
        foreach ($agegroup as $group) {
            if (in_array($group->slug, ['age-2', 'age-3', 'age-4'])) { ?>
                <a href="/preschool">
                <div class="blue-btn">
                    More Preschool Camps
                </div>
                </a> <?php ;
                break;
            }
        }
        foreach ($agegroup as $group) {
            if (in_array($group->slug, ['1st-grade', '2nd-grade', '3rd-grade', '4th-grade', '5th-grade', '6th-grade'])) { ?>
                <a href="/grades-k-6">
                <div class="blue-btn">
                    More Grade K-6 Camps
                </div>
                </a> <?php ;
                break;
            }
        }
        foreach ($agegroup as $group) {
            if (in_array($group->slug, ['7th-grade', '8th-grade', '9th-grade', '10th-grade', '11th-grade', '12th-grade'])) { ?>
                <a href="/teen">
                <div class="blue-btn">
                    More Teen Adventures
                </div>
                </a> <?php ;
                break;
            }
        }
    }
?>
Anonymous
  • 11,748
  • 6
  • 35
  • 57