0

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="http://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="http://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="http://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="http://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>
      <li data-target="#myCarousel" data-slide-to="1"><a href="#">Projects<small>Lorem ipsum dolor sit</small></a></li>
      <li data-target="#myCarousel" data-slide-to="2"><a href="#">Portfolio<small>Lorem ipsum dolor sit</small></a></li>
      <li data-target="#myCarousel" data-slide-to="3"><a href="#">Services<small>Lorem ipsum dolor sit</small></a></li>
    </ul>


</div><!-- End Carousel -->

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 href="<?php the_permalink(); ?>"><?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 href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                <?php endforeach; ?>

    </ul>


</div><!-- End Carousel -->

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;
});

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

2

There's probably some better way of doing this, but thought i'd give it a go anyways :) - also got some of the answer from here Adding active class to first item

<div class="container">
    <div id="myCarousel" class="carousel slide" data-ride="carousel">
        <!-- Wrapper for slides -->
        <div class="carousel-inner">
            <?php
            $c = 0;
            $class = '';
            $args = array( 'posts_per_page' => 4);
            $myposts = get_posts( $args );
            foreach ( $myposts as $post ) : setup_postdata( $post );
                $c++;
                $class = ($c == 1) ? 'active' : '';
                ?>
                <div class="item <?php echo $class; ?>">
                    <img src="http://placehold.it/1200x440/999999/cccccc">
                    <div class="carousel-caption">
                        <?php the_title(); ?>
                        <p><?php get_the_excerpt(); ?></p>
                    </div>
                </div><!-- End Item -->
            <?php
            endforeach;
            wp_reset_postdata();
            ?>
        </div><!-- End Carousel Inner -->
    </div>
        <ul class="nav nav-pills nav-justified">
            <?php
            $c = 0;
            $class = '';
            $i = 0;
            $args = array( 'posts_per_page' => 4);
            $myposts = get_posts( $args );
            foreach ( $myposts as $post ) : setup_postdata( $post );
                $c++;
                $class = ($c == 1) ? 'active' : '';
                ?>
                <li data-target="#myCarousel" data-slide-to="<?php echo $i++ ?>" class="<?php echo $class; ?>"><a href="#"><?php the_title(); ?></a></li>
                <?php
            endforeach;
            wp_reset_postdata();
            ?>
        </ul>
    </div><!-- End Carousel -->

    <script>
        $(document).ready( function() {
            $('#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>
| improve this answer | |
  • Hi, THank you, it;s working good :) Just one thing: when the slider - slide - I would like the tab related to the slide changing style . At the moment only the first tab get the active class . . . when slider change, the other tabs doesnt get the active class . .. any way to fix this that you may know ? – tibewww Aug 3 '15 at 12:05
  • Hey @tibewww - will try and look at it later when I get home! – Andy Aug 3 '15 at 12:52
  • Hi @tibewww - I think you might have some CSS declarations that are interfering with it - i've just tested on a completely fresh install / empty theme and the tabs do scroll for me... couple of things to make sure - are you calling your jquery first, then bootstrap.js then that final bit of script? – Andy Aug 4 '15 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.