Robson Robson - 2 months ago 14
PHP Question

Adding active class to first item in wordpress loop

I created slider in wordpress on category and post thumbnail.

I tried to add active class to first item in my loop, but loop show this class in all items.

How can I fix that ?

Here is my loop:

<div class="carousel-inner" role="listbox">

<?php
$c = 0;
$class = '';
query_posts('category_name=slider&showposts=3');
if ( have_posts() ) : while ( have_posts() ) : the_post();
$c++;

$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail' );
$url = $thumb['0'];

if ( $c == 1 ) $class .= ' active';
?>
<div class="item <?php echo $class; ?>">
<img src="<?php echo $url; ?>" class="img-responsive" alt="...">
<div class="carousel-caption">
<h2><?php the_content() ?></h2>
</div>
</div>
<?php
endwhile;endif;
wp_reset_query();
?>

</div>


http://pastebin.com/R5XA3ik9

Answer

Try with this

 if ( $c == 1 ){ $class = ' active';}
else{ $class='';}