个性化阅读
专注于IT技术分析

将活动类添加到wordpress循环的第一项

我在wordpress上创建了滑块, 然后发布了缩略图。

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

我该如何解决?

这是我的循环:

<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


#1


试试这个

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

OR

仅一行

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

#2


另一个答案是正确的,

你可以使用以下代码使代码更好

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

#3


$ class变量仍然具有第一个”回合”中的值。 (此外, 由于你正在使用。=, 它将变为活动状态…第n次)你需要添加else语句以删除旧值。

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

#4


请尝试一下。

<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>

#5


请尝试这个

`

<?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();
?>

`


#6


尝试这个

<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();
?>
赞(0)
未经允许不得转载:srcmini » 将活动类添加到wordpress循环的第一项

评论 抢沙发

评论前必须登录!