Search code examples
javascriptjquerywordpresscustom-post-typetestimonials

Adding navigation links on custom post type slider


I'm working on a Wordpress theme from scratch. I have a few sections, one of them showing a video and some testimonials. I've managed to construct (like this: http://www.wpbeginner.com/wp-tutorials/how-to-add-rotating-testimonials-in-wordpress/) a nice dynamic testimonial slider using custom post types (where each testimonial is a post) and a script that hide and show each post, making a simple but interesting transition.

Now I need to add a Carousel-like navigation under the slider (with those nice little dots). The problem is: I've tried many solutions but none of them worked. Can someone shine a light for me?

Here is the code I'm using to display the loop of the custom posts:

<div class="depo-wrap">
   <div class="depoimentos">
   <?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") { ?>
         <div class="slide" style="display: none;">
            <div class="citacao"><?php the_content(); ?></div>
            <div class="info-pessoal"><?php echo $data[ 'nome-pessoa' ]; ?><span> | </span><?php echo $data[ 'local-origem' ]; ?></div>
            <div class="cargo"><?php echo $data[ 'cargo' ]; ?></div>
         </div>
      <?php } else { ?>
         <div class="slide">
            <div class="citacao"><?php the_content(); ?></div>
            <div class="info-pessoal"><?php echo $data[ 'nome-pessoa' ]; ?><span> | </span><?php echo $data[ 'local-origem' ]; ?></div>
            <div class="cargo"><?php echo $data[ 'cargo' ]; ?></div>
         </div>

    <?php 
       $count++; } 
       endwhile; 
       endif; ?>
    </div>
</div>

And here is the script that makes everything work:

$(document).ready(function(){
  $('.depoimentos .slide');
  setInterval(function(){
    $('.depoimentos .slide').filter(':visible').fadeOut(1000,function(){
      if($(this).next('.slide').size()){
        $(this).next().fadeIn(1000);
      } else {
        $('.depoimentos .slide').eq(0).fadeIn(1000);
      }
    });
  }, 15000); 
});

TL;DR: I have this code here and I'm trying to add some of those Carousel-like bullets, or even some next/prev buttons. I just want to add a navigation to this testimonial slide that I've created with custom post types in Wordpress.


Solution

  • Adding the Bootstrap Carousel worked (thanks @Enrico)! Here's the code with some modifications:

    <div id="carousel-depoimentos" class="carousel slide carousel-fade" data-ride="carousel" data-interval="1000">
       <div class="carousel-inner depoimentos">
          <?php
             $args = array( 'post_type' => 'testimonial', 'posts_per_page' => 1 );
             $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 );
          { ?>
    
       <div class="item active">
          <div class="citacao"><?php the_content(); ?></div>
          <div class="info-pessoal"><?php echo $data[ 'nome-pessoa' ]; ?><span> | </span><?php echo $data[ 'local-origem' ]; ?></div>
          <div class="cargo"><?php echo $data[ 'cargo' ]; ?></div>
       </div>
    
       <?php 
          }
          endwhile; 
          endif; 
          wp_reset_postdata();
       ?> 
    
       <?php
          $args = array( 'post_type' => 'testimonial', 'posts_per_page' => 10, 'offset' => 1 );
          $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 );
       { ?>
    
       <div class="item">
          <div class="citacao"><?php the_content(); ?></div>
          <div class="info-pessoal"><?php echo $data[ 'nome-pessoa' ]; ?><span> | </span><?php echo $data[ 'local-origem' ]; ?></div>
          <div class="cargo"><?php echo $data[ 'cargo' ]; ?></div>
       </div>
    
       <?php 
          }
          endwhile; 
          endif; 
          wp_reset_postdata();
       ?>
       </div>                       
    </div>