0

I'm currently using the following code in order to get subcategories in a category page and getting the posts foreach subcategory. In the part where it echos "span-12" (line 14) i want it to echo "span-12 last" for every other subcategory.

my code:

<?php  $cats = get_categories('child_of='.get_query_var('cat')); 

foreach ($cats as $cat) :

$args = array(
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1, // max number of post per category
'category__in' => array($cat->term_id)
);
$my_query = new WP_Query($args); 

    if ($my_query->have_posts()) : 
    echo '<div id = "seasonBlock" class = "span-12" >';
    echo '</br><h3 class = "seasonTitle" >'.$cat->name.'</h3>';

    while ($my_query->have_posts()) : $my_query->the_post(); ?>     
    <?php /*general loop output; for instance: */ ?>
    <div id = "episodeBlock" >
    <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>  
    aired <?php if(!function_exists('how_long_ago')){the_time('F jS, Y'); } else { echo how_long_ago(get_the_time('U')); } ?><br />  
    </div>
    <?php endwhile; ?>
    </div>
    <?php else : 
    echo 'No Posts for '.$cat->name;                
    endif; 
wp_reset_query();

endforeach; ?>

In this part, (line 14)

 echo '<div id = "seasonBlock" class = "span-12" >';

i want to echo for every other category foreach, i want it to echo like this

 echo '<div id = "seasonBlock" class = "span-12 last" >';

I found some code for adding and checking if it is odd, but that doesn't seem to work for this code.

2

I wrote a function to help with this to functionally mimic Ruby on Rails' cycle helper:

/**
 * Cycle/alternate unlimited values of a given array.
 * 
 * For instance, if you call this function five times with
 * `cycle_it( 'three', 'two', 'one' )`, you will in return get:
 *    three two one three two
 * 
 * This is useful for loops and allows you to cycle classes.
 *
 * Example:
 * 
 *     foreach ( $posts as $post ) {
 *         printf(
 *             '<div class="%s">...</div>',
 *             esc_attr( cycle_it( 'odd', 'even' ) )
 *         );
 *     }
 * 
 * This would alternate between `<div class="odd">...</div>` and
 * `<div class="even">...</div>`.
 * 
 * You can pass any data as args and as many as you want, e.g.
 * 
 *     cycle_it( array( 'foo', 'bar' ), false, 5, 'silly' )
 *
 * @param mixed ...$args Any number of arguments to cycle.
 * @return mixed Alternating value passed through the function arguments.
 */
function cycle_it() {
    static $cycle_curr_index;
    $args = func_get_args();
    $fingerprint = sha1( serialize( $args ) );
    if ( ! is_array( $cycle_curr_index ) ) {
        $cycle_curr_index = array();
    }
    if ( ! is_int( $cycle_curr_index[ $fingerprint ] ) ) {
        $cycle_curr_index[ $fingerprint ] = -1;
    }

    $cycle_curr_index[ $fingerprint ] = ++$cycle_curr_index[ $fingerprint ] % count( $args );
    return $args[ $cycle_curr_index[ $fingerprint ] ];
}

If you add it to your theme's functions.php file, you can then change your code to this:

echo <div id="seasonBlock" class="span-12' . esc_attr( cycle_it( '',' last' ) ) . '" >';

1

You can use a loop counter to accomplish this. Set up a counter variable before you loop, check to see if it divided by 2 returns a 0 remainder (in this case) and adjust the class accordingly. Increment the counter at the end of each loop.

EDIT: updated code to use the built in counter in the foreach loop as per @goldenapples suggestion.

<?php  
$cats = get_categories('child_of='.get_query_var('cat'));
foreach ($cats as $counter => $cat) :
    $class = ($counter % 2) != 0 ? 'last' : '';
    // snip snip
    echo '<div id="seasonBlock" class="span-12 ' . $class . '" >';
    // snip snip
endforeach; 
?>
  • 1
    Even easier, just use an implied counter in the foreach loop: foreach ($cats as $counter => $cat) : (Note: this would start with 0, rather than 1 in your example) – goldenapples Feb 15 '12 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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