kitkat
kitkat

Reputation: 99

Different Output for "Every Other" Foreach Statement?

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.

Upvotes: 0

Views: 408

Answers (2)

ONOZ
ONOZ

Reputation: 1410

You can use the css-modifier's :first-child and :last-child to accomplish what you want using only css.

See this: http://www.quirksmode.org/css/firstchild.html

Upvotes: 0

nicholas.hauschild
nicholas.hauschild

Reputation: 42849

Use a boolean variable. At the end of each iteration of the loop set it to its oposite value:

//pseudo code
if (b) 
    outputFirst();
else 
    outputSecond();
b = !b;

Upvotes: 1

Related Questions