0

I have the following code:-

<ul class="grid effect-2" id="grid">

    <?php
    if( have_rows('gallery') ):
        while ( have_rows('gallery') ) : the_row(); ?>

            <?php
            $image_location = get_sub_field('image_location');

            if (empty($_GET['filter'])) {
                $image_filter = $image_location == 'Nottingham' || $image_location == 'Corby';
            } else {
                $image_filter = $image_location == $_GET['filter'];
            }

            ?>

            <?php 
            if($image_filter) {
            ?>

                <li><a class="fancybox" rel="gallery" href="<?php the_sub_field('image'); ?>" title="<?php echo $image_location . ' @ Planet Bounce ' . get_sub_field('image_location'); ?>"><img src="<?php the_sub_field('image'); ?>" alt="<?php echo get_sub_field('image_caption') . ' @ Planet Bounce ' . $image_location; ?>" /></a></li>

            <?php } ?>

        <?php endwhile;
    else : endif;
    ?>

</ul>

$image_location can either be 'Nottingham' or 'Corby' or both. The image filter is basically filtering which images are being shown.

Although the above code works, I don't think it is right to do it this way.

If anybody could help me with a better practise way of doing the same query that would be much appreciated.

If you need me to explain in better detail, please let me know.

0

1 Answer 1

1

The condition can be wrapped in a two liner code with use of array.

$imageFilters = array('Nottingham'=>'Nottingham', 'Corby'=>'Corby');
$image_filter = isset($imageFilters[$_GET['filter']]) ? $imageFilters[$_GET['filter']] :  $_GET['filter'];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.