0

这是我的代码,现在我想在第一篇文章中添加“活动”类。一些请帮我解决这个问题。我使用 $i 添加新课程。

<?php         
    $args = array( 'post_type' => 'cc_testimonial', 'posts_per_page' => 1, "order" => "DESC");
    $query = new WP_Query( $args );
    $cc = count($query);
    if ( $query->have_posts() ) {
    while ( $query->have_posts() ) { 
    $query->the_post();
?>
<?php for($i = 0; $i<$cc; $i++){ ?>
                    <div class="testimonialitem item <?php echo ($i==0)?'active':'' ?>">
                        <div class="testimonial-content">
                            <p><?php the_title( );?></p>
                        </div>                          
                    </div><!--/.testimonialitem-->                      


<?php    
}
            }
        }
        wp_reset_query();
        wp_reset_postdata();
?>
4

3 回答 3

3

要将活动课程应用于您的第一篇文章,请尝试以下代码:

<?php         
$args = array( 'post_type' => 'cc_testimonial', 'posts_per_page' => 1, "order" => "DESC");
$query = new WP_Query( $args );
$cc = count($query);
if ( $query->have_posts() ) {
$i=0;
while ( $query->have_posts() ) { 
$query->the_post();
<div class="testimonialitem item <?php echo ($i==0)?'active':''; ?>">
                    <div class="testimonial-content">
                        <p><?php the_title( );?></p>
                    </div>                          
                </div><!--/.testimonialitem-->       
      $i++; 
       }
    }
    wp_reset_query();
    wp_reset_postdata();
   ?>

谢谢。

于 2013-11-06T06:47:19.557 回答
0

尝试以下代码:

<?php         
$args = array( 'post_type' => 'cc_testimonial', 'posts_per_page' => 1, "order" => "DESC");
$query = new WP_Query( $args );
$cc = count($query);
if ( $query->have_posts() ) {
$i=0;
while ( $query->have_posts() ) { 
$query->the_post();
<div class="testimonialitem item <?php echo ($i==0)?'active':'' ?>">
                    <div class="testimonial-content">
                        <p><?php the_title( );?></p>
                    </div>                          
                </div><!--/.testimonialitem-->       
      $i++; 
       }
    }
    wp_reset_query();
    wp_reset_postdata();
   ?>
于 2013-11-06T06:13:28.927 回答
0

使用 jquery 为第一项添加类更容易。

尝试这个:

`$('.testimonialitem:first').addClass('active');`
于 2013-11-06T06:30:28.883 回答