首页 文章

Wordpress PHP循环构建自定义滑块

提问于
浏览
0

这是我第一次在这里发帖 . 我发现这个网站对帖子很有帮助 . 我非常感谢开发人员必须互相支持的奉献精神 .

我正在构建一个wordpress中的推荐滑块,以获得一些背景知识(如果需要)我已经遵循;如何在WordPress中添加旋转推荐作为启动器 . 然后使用Boostrap编写推荐滑块,Simple Bootstrap Testimonial Carousel(codepen.io/danielmdesigns/pen/yNzJwB)

我对HTML / Jquery方面感到满意 . 这是PHP循环给了我悲伤 . PHP来自WP Beginner的Wordpress中的How to Ad Rotating Testimonials,并尝试修改它以供我自己使用 .

我已经在PHP代码中对这个问题做了评论,由于我的PHP知识有限,我希望每个幻灯片只发布一个推荐,但是从试验和错误来看它与'php else'语句有关 . 目前在滑块中它会释放存储在CMS中的所有推荐,其中每个滑块应释放一个 . 如果查看数组代码,它上面有一个计数器 .

对于endwhile和end if语句,它是相同的,我猜测需要将某些东西添加到php else语句中,但不确定if语句的类型 .

<div class="carousel slide"  id="quote-carousel" data-ride="carousel">

<!-- Bottom Carousel Indicators -->
<ol class="carousel-indicators">
  <li data-target="#quote-carousel" data-slide-to="0" class="active"></li>
  <li data-target="#quote-carousel" data-slide-to="1"></li>
  <li data-target="#quote-carousel" data-slide-to="2"></li>
</ol>

<!-- Carousel Slides / Quotes -->
<div class="carousel-inner">

                <?php
                $args = array( 'post_type' => 'testimonial', 'posts_per_page' => 10 );
                $loop = new WP_Query( $args );
                if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();
                $data = get_post_meta( $loop->post->ID, 'testimonial', true );
                static $count = 0;
                if ($count == "1") { ?>

<!-- Quote 1 -->
<div class="item active">
  <div class="row">
    <div class="col-sm-12">
      <p><?php the_content(); ?></strong></small>

    </div>
  </div>
</div> 




<!-- Quote 2 -->
<div class="item">
  <div class="row">
    <div class="col-sm-12">


<!-- this php else is the issue, one testimonial should only release per slide -->
    <?php } else  { ?>
      <p><?php the_content(); ?>
</p>
      <small><strong></strong></small>
    </div>
  </div>
</div>
    <?php
$count++; }
endwhile;
endif; ?>
</div>

我知道这是可能的,我只是不知道如何 . 我们将非常感激地收到任何指导 .

3 回答

  • 0

    试试这个:

    <div class="carousel-inner">
    
                    <?php
                    $args = array( 'post_type' => 'testimonial', 'posts_per_page' => 10 );
                    $loop = new WP_Query( $args );
                    if ( $loop->have_posts() ){
                       while ( $loop->have_posts() ){
                             $loop->the_post();
                             $data = get_post_meta( $loop->post->ID, 'testimonial', true ); ?>
    
     <!-- Quote 1 -->
    <div class="item active">
      <div class="row">
        <div class="col-sm-12">
          <p><?php the_content(); ?></strong></small>
    
        </div>
      </div>
    </div> 
                 <?php      }
                    } ?>
    </div>
    

    看起来你似乎没有其他的需要,除非你有没有见证的时候 . 在这种情况下,在else {}内部,您可以显示替代内容 .

    我试图简单地让它更清楚地发生了什么 . 现在循环内部,每次有证词时,它会重复 <div class="item active"> div并在每次循环时放置当前推荐的内容 . 所以它会遍历前10个(因为你有 'posts_per_page' => 10 )并且每次有证据时都会打印出新的幻灯片 .

    我已经包含了 $data = get_post_meta( $loop->post->ID, 'testimonial', true ); 行,但我没有't see where you use this in the code. The data variable will only hold content if there is a custom field in the testimonial post type with the ID '推荐 .

  • 0

    我'd like to close this post, as I'已找到http://thecodeblock.com/testimonial-slider-using-bootstrap-carousel/的替代教程

  • 0

    你需要编写和条件语句让活动类循环滑动,这是我使用的技术,让它使用自定义帖子类型 .

    <div class="testimonials" id="testimonials">
    <div class="container-fluid">
        <div class="row no-padding">
            <div class="testimonial-content">
                <div class="col-md-push-2 col-md-3">
                  <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
    
                    <!-- Wrapper for slides -->
                    <div class="carousel-inner" role="listbox">
                      <?php 
    
                        if( $query->have_posts() ) : 
                          while( $query->have_posts() ) : 
                            $query->the_post(); 
                            $i++;
                      ?>
    
                        <div class="item <?php if ( $i == 1 ) {echo 'active';} ?>">
    
                          <p><?php the_field('testimonial'); ?></p>
                          <div class="testimonials-image">
                              <img class="img-responsive" src="<?php the_field('testimonial_image'); ?>" alt="">
                          </div>
                          <h5><?php the_field('testimonial_name'); ?></h5>
                          <h6><?php the_field('testimonial_occupation'); ?></h6>
    
                        </div>
    
                      <?php 
                        endwhile; 
                          endif; 
                            wp_reset_postdata(); 
                      ?>
    
                    </div>
    
                    <!-- Controls -->
                    <a class="left" href="#carousel-example-generic" role="button" data-slide="prev">
                      <i class="fa fa-long-arrow-left" aria-hidden="true"></i>
                      <span class="sr-only">Previous</span>
                    </a>
                    <a class="right" href="#carousel-example-generic" role="button" data-slide="next">
                      <i class="fa fa-long-arrow-right" aria-hidden="true"></i>
                      <span class="sr-only">Next</span>
                    </a>
    
                  </div>
                </div>
            </div>
        </div>
    </div>
    

相关问题