Search code examples
phpwordpressexpressionengine

Equivalent of ExpressionEngine switch tag in wordpress/php?


In ExpressionEngine while in their version of 'the loop' I can add a tag to any element like this:

<li class="{switch='one|two|three|four|five|six'}">

The first iteration of the li will have class one, the next is two, and the loop again after six. I'm needing this similar functionality in a wordpress site, but not sure how to accomplish that. Is there a built in wordpress function or will I need to code some sort of function in php?

Currently, using this in attempt at using @Leonard's solution, but the class 'four' is being repeated over and over instead of cycling

<?php

$argsGallery = array(
    'post_type' => 'gallery',
    'orderby'       => 'menu_order',
    'order'         => 'ASC'
);
$the_query = new WP_Query( $argsGallery );

// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();?>

            <div class="<?php cycle('four|three|five|two|six|four'); ?> columns">
                <div class="thumb">

                    <a class="jackbox" 
                        data-group="images" 
                        data-thumbnail="<?php the_field('image'); ?>" 
                        data-title="Image One" 
                        data-description="#description_1" 
                        href="<?php the_field('image'); ?>"
                    >
                        <div class="jackbox-hover jackbox-hover-black">
                            <p><?php the_field('image_description'); ?> </p>
                        </div>
                        <img 
                            src="<?php the_field('image'); ?>" 
                            alt="responsive lightbox" 
                        />
                    </a>
                </div>
            </div>

<?php
    endwhile;
    wp_reset_query();
    wp_reset_postdata();
?>

Solution

  • Found this question whilst looking for the exact same thing... top of Google 20mins after you posted it. Crazy... anyway!

    I've come up with a function that I've tested (albeit quickly) that you can drop in to your functions.php and it works with a standard Wordpress loop. It may need adapting for some needs but hopefully it's a good start point.

    It uses the current_post count from the $wp_query array and works out where it needs to be in the cycle values.

    function cycle($input, $delimiter = '|', $query = false) {
    
        if($query == false):
            global $wp_query ;
            $current_post = $wp_query->current_post + 1;
        else:
            $current_post = $query->current_post + 1;
        endif;
    
    
        $switches = explode($delimiter, $input);
    
        $total_switches = count($switches) ;
        $current_set = ceil( $current_post / $total_switches)  ;
    
        $i = (($current_post - ($current_set * $total_switches)) + $total_switches) - 1 ;
    
        echo $switches[$i];
    
    }
    

    Then you can use it in a STANDARD loop like so:

     <?php cycle('first|second|third|fourth'); ?>
    

    Or you can custom delimit if needs be:

     <?php cycle('first*second*third', '*'); ?>
    

    Or if your using it with a CUSTOM wp_query you must use it like this with the query fed in as the third argument:

     <?php cycle('first|second|third', '|', $the_query); ?>
    

    I'm sure there is a tidier way to feed in that custom query, I'll keep looking and update if/ when I find a way!