Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
1  
This appears to be a PHP question, not a WordPress question. Recommended reading. –  Chip Bennett Feb 15 '12 at 15:43
add comment

2 Answers

I wrote a function to help with this to functionally mimic RoR's 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.
 * For instance, foreach ($posts as $post) { echo '<div class="'.cycle_it('odd','even').'">...</div>'; }
 * would alternate between <div class="odd">...</div> and <div class="even">...</div>. Neat, huh?
 * 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 Accepts unlimited args
 * @return mixed
 * @author Matthew Boynes
 */
function cycle_it() {
    global $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 functions.php file, you can then change your code to this:

echo '<div id = "seasonBlock" class = "span-12'.cycle_it('',' last').'" >';
share|improve this answer
    
best code ever! thanks man. –  user1178152 Feb 16 '12 at 12:18
add comment

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; 
?>
share|improve this answer
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
    
@goldenapples good call! –  chrisguitarguy Feb 15 '12 at 19:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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