0

I have created a loop to display programs. One of the taxonomies for each program is a grade or grades. Some programs are for 1st and 2nd graders, for example. I would like to display a div in my results that says "Grades 1-2". However, the code I am using is displaying this logic for the programs that are Grades K-2 and K-6, since they include grades 1 and 2 as well. How can I adjust the IF statement so that it only displays camps that have exactly grades 1 and 2?

Here is my code:

        <div class="sc-age-group">
                <?php 
                     $agegroup = wp_get_post_terms(get_the_ID(), 'camper_grade');
                     if ($agegroup) {
                        foreach ($agegroup as $group) {
                            if ($group->name == "1st Grade" && "2nd Grade") { ?>
                                <div class="sc-age-group_1-2">Grades 1-2</div> <?php ;
                            }
                        }
                     }
                ?>
         </div>

1 Answer 1

0

You must use the || code

not && I also deleted this code that was wrong in your code

as ; in the PHP code opener

Code Like This :


        <div class="sc-age-group">
                <?php 
                     $agegroup = wp_get_post_terms(get_the_ID(), 'camper_grade');
                     if ($agegroup) {
                        foreach ($agegroup as $group) {
                            if ($group->name == "1st Grade" || $group->name == "2nd Grade") { ?>
                                <div class="sc-age-group_1-2">Grades 1-2</div> <?php 
                            }
                        }
                     }
                ?>
         </div>
3
  • @fridaus Rudy - sorry, that's not it. Changing to OR operator results in listing the intended title each time a grade is selected and doesn't exclude programs that have grades 1 and 2 and additional grade(s). Any other ideas?
    – Iisrael
    Commented Nov 11, 2019 at 19:50
  • Hi @Iisrael sorry if that's wrong, but the code you demonstrated is not enough to make me understand what you want to display. I only focus on your question at the end of your sentence and code snippet. Can you explain in more detail? Do you mean displaying options? Commented Nov 12, 2019 at 18:04
  • Thank you for following up. I will try to explain better here. My taxonomy has many terms (Preschool, Kindergarten, 1st Grade, 2nd Grade, 3rd Grade and so on). Some programs include the terms Kindergarten, 1st Grade, and 2nd Grade. Others ONLY have 1st Grade and 2nd Grade. These are the ones that I would like to display. So the logic needs to find programs with just those two grades, NOT those two plus any others. Does that make sense?
    – Iisrael
    Commented Nov 12, 2019 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.