• I’m trying to add a carousel with my post. UNder the carousel, some tabs with the post title.

    On click of the post title, it should change it the slide to display the related post.

    In ” Static” in works perfectly, but when I put the wordpress loop, the slider stop to work :(.

    Any help would be really amazing ?? ??

    I attach the code in static which works fine and the one with my loop added if it can help…..

    Html:

    <div class="container">
        <div id="myCarousel" class="carousel slide" data-ride="carousel">
    
          <!-- Wrapper for slides -->
          <div class="carousel-inner">
    
            <div class="item active">
              <img src="https://placehold.it/1200x440/cccccc/ffffff">
               <div class="carousel-caption">
                <h3>Headline</h3>
                <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</p>
              </div>
            </div><!-- End Item -->
    
             <div class="item">
              <img src="https://placehold.it/1200x440/999999/cccccc">
               <div class="carousel-caption">
                <h3>Headline</h3>
                <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</p>
              </div>
            </div><!-- End Item -->
    
            <div class="item">
              <img src="https://placehold.it/1200x440/dddddd/333333">
               <div class="carousel-caption">
                <h3>Headline</h3>
                <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</p>
              </div>
            </div><!-- End Item -->
    
            <div class="item">
              <img src="https://placehold.it/1200x440/999999/cccccc">
               <div class="carousel-caption">
                <h3>Headline</h3>
                <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</p>
              </div>
            </div><!-- End Item -->
    
          </div><!-- End Carousel Inner -->
    
    		<ul class="nav nav-pills nav-justified">
              <li data-target="#myCarousel" data-slide-to="0" class="active"><a href="#">About<small>Lorem ipsum dolor sit</small></a>
              <li data-target="#myCarousel" data-slide-to="1"><a href="#">Projects<small>Lorem ipsum dolor sit</small></a>
              <li data-target="#myCarousel" data-slide-to="2"><a href="#">Portfolio<small>Lorem ipsum dolor sit</small></a>
              <li data-target="#myCarousel" data-slide-to="3"><a href="#">Services<small>Lorem ipsum dolor sit</small></a>
    
        </div><!-- End Carousel -->
    </div>

    with my wordpress loop:

    <div class="col-md-12">
        <div id="myCarousel" class="carousel slide" data-ride="carousel">
    
          <!-- Wrapper for slides -->
          <div class="carousel-inner">
    
        <?php
        global $post;
        if(is_category() || is_single()){
        foreach(get_the_category() as $category)
        {
        $current = $category->cat_ID;
        $current_name = $category->cat_name;
    
        //query_posts("cat=". $current);
    
        $myposts = get_posts(array( 'offset' => 1, 'category__in' => array($current)));
    
        //query_posts(array('category__in' => array(11)));
            }
        }
    
        foreach($myposts as $post) :
        setup_postdata($post); 
    
        ?>
            <div class="item active">
                                <div class="punica-zoom-effect"><a>"><?php the_post_thumbnail(''); ?></a></div>
               <div class="carousel-caption">
                <h3><?php the_title(); ?></h3>
                <p><?php the_excerpt(); ?></p>
              </div>
            </div><!-- End Item -->
    
              <?php endforeach; ?>
    
          </div><!-- End Carousel Inner -->
    
            <ul class="nav nav-pills nav-justified">
    
        <?php
        global $post;
        if(is_category() || is_single()){
        foreach(get_the_category() as $category)
        {
        $current = $category->cat_ID;
        $current_name = $category->cat_name;
    
        //query_posts("cat=". $current);
    
        $myposts = get_posts(array( 'offset' => 1, 'category__in' => array($current)));
    
        //query_posts(array('category__in' => array(11)));
            }
        }
    
        foreach($myposts as $post) :
        setup_postdata($post); 
    
        ?>
              <li data-target="#myCarousel" data-slide-to="0" class="active"><a>"><?php the_title(); ?></a>
                        <?php endforeach; ?>
    
        </div><!-- End Carousel -->
    </div>

    My Jquery:

    <script>
        $('#myCarousel').carousel({
            interval:   4000
        });
    
        var clickEvent = false;
        $('#myCarousel').on('click', '.nav a', function() {
                clickEvent = true;
                $('.nav li').removeClass('active');
                $(this).parent().addClass('active');
        }).on('slid.bs.carousel', function(e) {
            if(!clickEvent) {
                var count = $('.nav').children().length -1;
                var current = $('.nav li.active');
                current.removeClass('active').next().addClass('active');
                var id = parseInt(current.data('slide-to'));
                if(count == id) {
                    $('.nav li').first().addClass('active');
                }
            }
            clickEvent = false;
        });
    </script>

    Thank you for your help Guys, I hope everything make sense ??

  • The topic ‘add bootstrap post slider with tabs’ is closed to new replies.