将 active class 添加到 wordpress 循环中的第一项

Adding active class to first item in wordpress loop

我在 wordpress 中的类别和 post 缩略图上创建了滑块。

我试图将活动 class 添加到循环中的第一个项目,但在所有项目中循环显示此 class。

我该如何解决?

这是我的循环:

<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

$class 变量仍然具有第一个 "round" 的值。 (此外,由于您正在使用 .= 它将成为 active active ... 第 n 次) 您需要添加 else 语句才能删除旧值。

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

试试这个

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

就在一行中

$class = ($c == 1) ? 'active' : '';

其他回答正确,

您可以使用

使这段代码更好
$class = ($c == 1) ? 'active' : '';

试试这个

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

<?php
    $c = 0;

    query_posts('category_name=slider&showposts=3');
    if ( have_posts() ) : while ( have_posts() ) : the_post();

$class = ''; $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 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';
            else $class = '';
                ?>
                 <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>

请试试这个

`

<?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'];

        $class = ($c == 1) ? '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();
?>

`