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

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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效