Frage

I want a field to show if it has a category of either 2 values from 2 different sub fields.

Im not quite sure how to do this though with 2 different subfields.

Here is an exmaple im trying to demo to show how I would like it to work:

<?php while(has_sub_field('team_profile')): 
    $category = get_sub_field('category');
    $category_2 = get_sub_field('category_2');
    if($category=='copyclearance')
        if($category_2=='anothercat'){ ?>
            <li class="col-lg-2 teamProfile">
                <img src="<?php the_sub_field('profile_image'); ?>" class="img-responsive"/>
                <h2><?php the_sub_field('profile_name'); ?></h2>
                <p class="jobTitle"><?php the_sub_field('job_title'); ?></p>
            </li>
            <?php 
        } 
endwhile; ?>

This is my working version with just one category filter (but I need 2):

<?php while(has_sub_field('team_profile')): 
    $category = get_sub_field('category');
    if ($category=='copyclearance') { ?>
        <li class="col-lg-2 teamProfile">
            <img src="<?php the_sub_field('profile_image'); ?>" class="img-responsive"/>
            <h2><?php the_sub_field('profile_name'); ?></h2>
            <p class="jobTitle"><?php the_sub_field('job_title'); ?></p>
        </li>
    <?php 
    } 
endwhile; ?>

So this would work, by looking to see if anything has been posted in either $category or in $category_2 then displaying all of them results. NOT using the 2 to filter specifically i.e only showing posts that are in both $category and $category_2

War es hilfreich?

Lösung

Instead of nesting your if statements, use the or operator:

if($category=='copyclearance' || $category=='anothercat') {
    // ...
}

If you want to do more than two, I recommend using an array:

$valid_cats = array('copyclearance', 'cat2', 'cat3', 'etc');

if(in_array($category, $valid_cats)) {
    // ...
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top