doucigua0449
2018-07-12 05:46
浏览 103
已采纳

Bootstrap轮播不适用于foreach循环

  <div class="carousel-inner">
    <?php $output = '';
    $class = 'carousel-item';
    $query = new WP_Query( array( 'post_type' => 'testimonials' , 'posts_per_page' => -1,) ); 
    $posts = $query->posts; 
    foreach($posts as $post)  {    
      $active = ($k == 0) ? " active" : "";   
      $output .=
      $active = ($k == 0) ? " active" : "";   
      $output .=
      "<div class='{$class}{$active}'> 
        <p><?php echo the_content();?></p>
        <h4><?php echo the_title();?></h4>
      </div>"; } ?>
    <div class="carousel-item">
      <p><?php echo the_content();?></p>
      <h4><?php echo the_title();?></h4>
    </div>
  </div>

It doesnot display the output I don't know where I'm wrong please help foreach for active and inactive class

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongye8110 2018-07-12 06:06
    已采纳

    $i is not properly used you should use it before the starting of your loop, also $k is not defined you structure is not proper.

    Please use below code and let me know if it works.

                        <div id="carouselExampleIndicators2" class="carousel slide" data-ride="carousel">
            <!--This loop is fetching correct data -->
            <ol class="carousel-indicators">
              <?php $the_query = new WP_Query( array( 'post_type' => 'testimonials' , 'posts_per_page' => -1,) ); 
                // The Loop
                if ( $the_query->have_posts() ) {
                  $i = 0;
                    while ( $the_query->have_posts() ) {
                        $the_query->the_post();
                    if($i == 0) {
                      $activeClass = 'active';
                    }
                    else {
                      $activeClass = '';
                    }
                        echo '<li data-id="'.$post->ID.'" data-target="#carouselExampleIndicators2" data-slide-to="'.$i.'" class="'.$activeClass.'">
                            <h2>'.get_the_title().'</h2>
                            <p>'.trim(strip_tags(get_the_content())).'</p>
                          </li>';
                    $i++;
                    }
                    /* Restore original Post Data */
                    wp_reset_postdata();
                }               
              ?>
            </ol>
          <div class="carousel-inner">
            <?php 
            $output = '';
            $class = 'carousel-item';
            $the_query = new WP_Query( array( 'post_type' => 'testimonials' , 'posts_per_page' => -1,) );             
            // The Loop
            if ( $the_query->have_posts() ) {
              $i = 0;
              while ( $the_query->have_posts() ) {
                $the_query->the_post();
                if($i == 0) {
                  $activeClass = 'active';
                }
                else {
                  $activeClass = '';
                }
                echo "<div class='{$class}{$activeClass}'> 
                  <p>".get_the_content($post->ID)."</p>
                  <h4>".get_the_title($post->ID)."</h4>
                </div>"; 
                $i++;
              }
              /* Restore original Post Data */
              wp_reset_postdata();
            }            
            ?>            
          </div>
        </div>
    
    点赞 打赏 评论

相关推荐 更多相似问题