Skip to content
Advertisement

How to prevent duplicate results in For Each loop?

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 ;
            }
        }
    }
?>

Advertisement

Answer

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;
            }
        }
    }
?>
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement