hello i tried to show 3 slide per page with bootstrap carousel but when I use wordpress php query it showed just 1 slide per page :( and for show another slide i should click on next... please see this http://bootsnipp.com/snippets/featured/responsive-moving-box-carousel
I tried to code this by php query .
my php code :
<div class="carousel slide" id="myCarousel">
<div class="carousel-inner">
<?php
$counter=0;
?>
<div class="item <?php if($counter==0){echo 'active';} ?>">
<ul class="thumbnails">
<?php
$my_query = new WP_Query('showposts=3&cat=1');
while ($my_query->have_posts()):
$my_query->the_post();
$do_not_duplicate = $post->ID;?>
<li class="col-sm-4">
<div class="shakhes">
<?php the_post_thumbnail('shakhes') ?>
<div class="caption"><a href="<?php the_permalink(); ?>">
<h4><div class="livicon" data-name="chevron-left" data-size="30" data-color="#555" data-hovercolor="#555" ></div><?php the_title(); ?></h4></a>
<p><?php the_content_rss('', TRUE, '', 20); ?></p>
<div class="moreshakhes"> مشاهده ادامه مطلب</div>
</div>
</div>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php $counter++; ?>
</div>
<nav>
<ul class="control-box pager">
<li><a data-slide="prev" href="#myCarousel" class=""><i class="glyphicon glyphicon-chevron-right"></i></a></li>
<li><a data-slide="next" href="#myCarousel" class=""><i class="glyphicon glyphicon-chevron-left"></i></a></li>
</ul>
</nav>
<!-- /.control-box -->
</div><!-- /#myCarousel -->
You have written a wrong sctipt and used many depricated wordpress functions. the_content_rss function is deprecated and use the_content_feed and to show posts you have to use 'posts_per_page' instead showposts . I have rewritten your script with proper functions. Copy it from below and paste it to your template.I have checked the script and its working. Let me know if something wrong happen with you..
<div class="carousel slide" id="myCarousel">
<div class="carousel-inner">
<?php
$counter=0;
?>
<div class="item <?php if($counter==0){echo 'active';} ?>">
<ul class="thumbnails">
<?php
$my_query = new WP_Query(array(
'posts_per_page' => 5,
'cat' => '1',
)
);
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;?>
<li class="col-sm-4">
<div class="shakhes">
<?php the_post_thumbnail('shakhes') ?>
<div class="caption"><a href="<?php the_permalink(); ?>">
<h4><div class="livicon" data-name="chevron-left" data-size="30" data-color="#555" data-hovercolor="#555" ></div><?php the_title(); ?></h4></a>
<p><?php the_content_feed('', TRUE, '', 20); ?></p>
<div class="moreshakhes"> مشاهده ادامه مطلب</div>
</div>
</div>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php $counter++; ?>
</div>
<nav>
<ul class="control-box pager">
<li><a data-slide="prev" href="#myCarousel" class=""><i class="glyphicon glyphicon-chevron-right"></i></a></li>
<li><a data-slide="next" href="#myCarousel" class=""><i class="glyphicon glyphicon-chevron-left"></i></a></li>
</ul>
</nav>
<!-- /.control-box -->
</div><!-- /#myCarousel -->
This is the solution I came up with:
<div id="myCarousel" class="carousel slide" data-ride="carousel" data-interval="0">
<div class="carousel-inner">
<?php $args = array(
'post_type' => 'product',
'posts_per_page' => 8,
);
$query = new WP_Query( $args );
$i = 1;
$next_item = true;
while($query->have_posts()) : $query->the_post();
if ($i == 1) {
echo '<div class="item carousel-item active">';
} elseif ($next_item == true) {
echo '<div class="item carousel-item">';
}
?>
<div class="col-sm-3">
<div class="thumb-wrapper">
<div class="img-box">
<?php the_post_thumbnail();?>
<h4><?php the_title();?></h4>
</div>
<div class="thumb-content">
<?php the_excerpt();?>
Read More
</div>
</div>
</div>
<?php
$next_item = false;
if ($i % 4 == 0) {
echo '</div>';
$next_item = true;
}
$i++;
endwhile
?>
</div>
<a class="carousel-control-prev pull-left" href="#myCarousel" data-slide="prev">
<i class="fa fa-angle-left"></i>
</a>
<a class="carousel-control-next pull-right" href="#myCarousel" data-slide="next">
<i class="fa fa-angle-right"></i>
</a>
</div>
Related
I am trying to display a piece of HTML code I added to a WordPress website but I do not want to use the echo function as it affects the loop. I had to use a WP_query which works in a certain way to get what I want to do. My code works great but I don't want it on certain pages.
This is what I have tried so far
<?php
if(!is_front_page()){
<div class="slider-container">
<h4 class="slider-header">
<span class="slider-header_text">Trending</span>
</h4>
<div class="slider">
<div class="carousel-container">
<div class="carousel-inner">
<div class="track">
<!--Beginning of responsive slider-->
<?php $custom_query = new WP_Query(array(
'posts_per_page' => 12,
'category_name' => 'featured-posts',
));
while($custom_query->have_posts()) : $custom_query->the_post(); ?>
<div class="card-container">
<a href="<?php the_permalink(); ?>" class="card">
<div class="img">
<img src="<?php the_post_thumbnail_url('thumbnail'); ?>" alt="">
</div>
<div class="info">
<h5><?php the_title(); ?></h5>
</div>
</a>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); // reset the query ?>
</div>
</div>
<div class="nav">
<button class="prev">
<i class="fas fa-angle-left"></i>
</button>
<button class="next">
<i class="fas fa-angle-right"></i>
</button>
</div>
</div>
</div>
</div>
}
?>
But this immediately throws an error.
I haven't checked if your code works. But the provided code without syntax errors would be:
<?php
if (!is_front_page()) {
?>
<div class="slider-container">
<h4 class="slider-header">
<span class="slider-header_text">Trending</span>
</h4>
<div class="slider">
<div class="carousel-container">
<div class="carousel-inner">
<div class="track">
<!--Beginning of responsive slider-->
<?php $custom_query = new WP_Query(array(
'posts_per_page' => 12,
'category_name' => 'featured-posts',
));
while ($custom_query->have_posts()) : $custom_query->the_post(); ?>
<div class="card-container">
<a href="<?php the_permalink(); ?>" class="card">
<div class="img">
<img src="<?php the_post_thumbnail_url('thumbnail'); ?>" alt="">
</div>
<div class="info">
<h5><?php the_title(); ?></h5>
</div>
</a>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); // reset the query
?>
</div>
</div>
<div class="nav">
<button class="prev">
<i class="fas fa-angle-left"></i>
</button>
<button class="next">
<i class="fas fa-angle-right"></i>
</button>
</div>
</div>
</div>
</div>
<?php } ?>
Am wanting to display Wordpress post items as slider, but they seem to be stacking and not sliding left to right, and carousel controls therefore won't work. If I make 'posts_per_page' => 1, the second post disappears but if I make 'posts_per_page' => 10, they stack. Any help will be appreciated.
Many thanks,
Cali
$post = get_field('post');
?>
<!-- OPEN CAROUSEL LOOP -->
<?php
$loop = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 1,
'orderyby' => 'post_id',
'order' => 'ASC',
)); ?>
<?php
while ( $loop->have_posts() ) : $loop->the_post();
?>
<!-- START CAROUSEL **************************************************************** -->
<div id="news-carousel" class="carousel slide" data-ride="carousel">
<!-- **************************************************************** -->
<!-- Indicators -->
<ul class="carousel-indicators">
<?php
$active = 'active';
$num = 0;
?>
<li data-target="#carousel1" data-slide-to="<?php echo $num ?>" class="<?php echo $active ?>"></li>
<?php
$active = '';
$num += 1;
?>
</ul>
<!-- **************************************************************** -->
<?php if ( has_post_thumbnail() ) { ?>
<!-- The slideshow -->
<div class="carousel-inner" role="listbox" >
<?php $active = 'active'; ?>
<div class="carousel-item <?php echo $active ?>">
<div >
<?php the_post_thumbnail( 'full' ); ?>
<h1><?php the_title(); ?></h1>
</div>
</div><!-- /item -->
<?php $active = ''; ?>
</div>
<!-- // End The slideshow -->
<?php } ?>
<!-- **************************************************************** -->
<!-- Left and right controls -->
<a class="carousel-control-prev" href="#news-carousel" role="button" data-slide="prev">
<i class="fa fa-chevron-left"></i> </a>
<a class="carousel-control-next" href="#news-carousel" role="button" data-slide="next">
<i style="color: black;" class="fa fa-chevron-right"></i> </a>
</div> <!-- Carousel 1 -->
<!-- End Carousel **************************************************************** -->
<?php
endwhile;
wp_reset_postdata();
?>
Try to use this code:
<!--OPEN CAROUSEL LOOP-->
<?php $loop = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 6,
'orderyby' => 'post_id',
'order' => 'ASC' ));
?>
<!--CAROUSEL indicators SECTION START HERE-->
<ul class="carousel-indicators">
<?php $count = 0; while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li data-target="#carousel1" data-slide-to="<?php echo $count ?>" class="<?php if($count == '0'){ echo 'active'; } ?>"></li>
<?php $count++; endwhile; wp_reset_postdata(); ?>
</ul>
<!--//CAROUSEL indicators SECTION END HERE-->
<!--CAROUSEL SLIDER SECTION START HERE-->
<div id="news-carousel" class="carousel slide" data-ride="carousel">
<?php $count = 0; while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php if ( has_post_thumbnail() ) { ?>
<!-- The slideshow -->
<div class="carousel-inner" role="listbox" >
<div class="carousel-item <?php if($count == '0'){ echo 'active'; } ?>">
<div >
<?php the_post_thumbnail( 'full' ); ?>
<h1><?php the_title(); ?></h1>
</div>
</div><!-- /item -->
</div>
<!-- // End The slideshow -->
<?php } ?>
<!-- **************************************************************** -->
<?php $count++; endwhile; wp_reset_postdata(); ?>
<!-- Left and right controls -->
<a class="carousel-control-prev" href="#news-carousel" role="button" data-slide="prev">
<i class="fa fa-chevron-left"></i> </a>
<a class="carousel-control-next" href="#news-carousel" role="button" data-slide="next">
<i style="color: black;" class="fa fa-chevron-right"></i> </a>
</div> <!-- Carousel 1 -->
<!--//CAROUSEL SLIDER SECTION END HERE-->
<!--//CAROUSEL indicators SECTION End HERE-->
Shammi, thanks to your help above, I reworked the code and managed to get it to work. Here is is:
$carousel_item = get_field('carousel_item');
?>
<!--OPEN CAROUSEL LOOP-->
<?php $loop = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 6,
'orderyby' => 'post_id',
'order' => 'ASC' ));
?>
<div id="demo" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ul class="carousel-indicators">
<?php $count = 0; while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li data-target="#news-carousel" data-slide-to="<?php echo $count ?>" class="<?php if($count == '0'){ echo 'active'; } ?>"></li>
<?php $count++; endwhile; wp_reset_postdata(); ?>
</ul>
<?php $count = 0; while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php if ( has_post_thumbnail() ) { ?>
<!-- The slideshow -->
<div class="carousel-item <?php if($count == '0'){ echo 'active'; } ?>">
<div >
<?php the_post_thumbnail( 'full' ); ?>
<h1><?php the_title(); ?></h1>
</div>
</div><!-- /item -->
<?php } ?>
<?php $count++; endwhile; wp_reset_postdata(); ?>
<!-- Left and right controls -->
<a class="carousel-control-prev" href="#demo" role="button" data-slide="prev">
<i class="fa fa-chevron-left"></i> </a>
<a class="carousel-control-next" href="#demo" role="button" data-slide="next">
<i style="color: black;" class="fa fa-chevron-right"></i> </a>
</div> <!-- Carousel 1 -->
So I've created a bootstrap carousel in WordPress and it works fine. My only problem is that when I click on the image it doesn't take me to the specific article. How Can I solve this? Here is my Code:
<div class="container slider-container">
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<?php $slider = get_posts(array('post_type' => 'post', 'posts_per_page' => 3)); ?>
<?php $count = 0; ?>
<?php foreach($slider as $slide): ?>
<div class="item <?php echo ($count == 0) ? 'active' : ''; ?>">
<a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id($slide->ID)) ?>" class="img-responsive"/>
</a>
</div>
<?php $count++; ?>
<?php endforeach; ?>
<!-- Left and right controls -->
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
Change <?php the_permalink(); ?> to <?php echo get_permalink($slide->ID); ?>.
Why:
the_permalink() gets the current permalink for the current post in the loop. You are not in the loop, you are simply querying posts. By changing to get_permalink() and passing an ID into the function, you are getting the specific permalink for this slide.
See get_permalink() here.
here is my purposition, i used it on my websites, hope to help you !
`<?php
$args = array(
'post_type' => 'slide',
'posts_per_page' => -1,
'order' => 'ASC',
);
$slide = new WP_Query($args);?>
<?php if ($slide->have_posts()): ?>
<div id="slider">
<div class="bd-example">
<div id="carouselExampleCaptions" class="carousel slide carousel-fade" data-ride="carousel" data-interval=10000>
<div class="">
<ol class="carousel-indicators">
<?php $i = 0;while ($slide->have_posts()): $publicity->the_post();?>
<li data-target="#carousel-example-generic" data-slide-to="<?php echo $i ?>" class="<?php if ($i === 0): ?>active<?php endif;?>"></li>
<?php $i++; endwhile;?>
</ol>
<div class="carousel-inner">
<?php $i = 0;while ($slide->have_posts()): $slide->the_post();?
<div class="carousel-item <?php if (0 == $i) {echo ' active';}?>" style="background:url('<?php the_post_thumbnail_url('full');?>') center center no-repeat; background-size: cover; min-height: 100vh;">
<div class="carousel-caption d-none d-md-block">
<div class="row align-items-center ">
<div class="col-lg-4 title">
<span><?php the_title();?></span>
<h2><?php the_content();?></h2>
</div>
</div>
</div>
</div>
<?php $i++;endwhile;?>
</div>
</div>
</div>
</div>
</div>
<?php wp_reset_postdata();endif;?>
`
I have designed a site which I am converting to wordpress site,as I am new to wordpress I am not able get images dynamically from wordpress for bootstrap image carousel
this is my code using<?php bloginfo(template_url); ?>
but i need posts: which goes something like this:
<?php
query_posts(array('category_name' => 'social', 'posts_per_page' => 5));
if(have_posts()) : while(have_posts()) : the_post()
?>
<li><?php the_post_thumbnail('social'); ?></li>
<?php endwhile; endif; wp_reset_query(); ?>
And here is my code for image slide
<div class=" col-xs-12 col-sm-12 col-md-12 col-lg-12 topmargin">
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>
<!-- Wrapper for slides -->
<?php
$qArgs = array(
'category_name' => 'mainslide',
'posts_per_page' => '5'
);
$newQuery = new WP_Query($qArgs); ?>
<div class="carousel-inner" role="listbox">
<?php if($newQuery->have_posts()) : ?>
<?php while($newQuery->have_posts()) : $newQuery->the_post(); ?>
<div class="item">
<?php echo get_the_post_thumbnail( $post->ID, 'full'); ?>
</div>
<?php endwhile;?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</div>
<!-- Controls -->
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
here is the screen shot
I recommend use wp_Query instead query_posts, for better understanding read this
Now, try this:
<?php
$qArgs = array(
'category_name' => 'social',
'posts_per_page' => '5'
);
$newQuery = new WP_Query($qArgs); ?>
<div class="carousel-inner" role="listbox">
<?php if($newQuery->have_posts()) : ?>
<?php while($newQuery->have_posts()) : $newQuery->the_post(); ?>
<div class="item">
<?php echo get_the_post_thumbnail( $post->ID, 'full'); ?>
</div>
<?php endwhile;?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</div>
I have integarted bootstrap carousel into my wordpress. The slides will be taken from the posts which will be tagged as "featured" so only 5 recently enter "featured" posts will be shown.
Below is my code:
<div id="carousel-captions" class="carousel slide bs-docs-carousel hidden-xs">
<ol class="carousel-indicators">
<li data-target="#carousel-captions" data-slide-to="0" class="active"></li>
<li data-target="#carousel-captions" data-slide-to="1" class=""></li>
<li data-target="#carousel-captions" data-slide-to="2" class=""></li>
<li data-target="#carousel-captions" data-slide-to="3" class=""></li>
<li data-target="#carousel-captions" data-slide-to="4" class=""></li>
</ol>
<div class="carousel-inner">
<?php $the_query = new WP_Query( 'tag=featured&orderby=date&posts_per_page=5' ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="item">
<a href="<?php the_permalink() ?>">
<img src="<?php the_field('header_banner', $post_id); ?>" alt="">
<div class="carousel-caption">
<h3><?php the_field('year', $post_id); ?></h3><span class="name">Make<br><?php $category = get_the_category(); echo $category[0]->cat_name; ?></span><hr>
<p><?php the_field('mileage', $post_id); ?> Miles | <?php the_field('exterior_color', $post_id); ?> Color<br><br><?php echo homepage_carousel_excerpt(15); ?></p><span class="btn btn-default">Details →</span>
</div></a>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</div>
<a class="left carousel-control" href="#carousel-captions" data-slide="prev">
<span class="icon-prev"></span>
</a>
<a class="right carousel-control" href="#carousel-captions" data-slide="next">
<span class="icon-next"></span>
</a>
</div>
The problem is It doesn't slide because the "active" class is not working statically.
HOw do I fix this?
Thanks
To avoid having to query twice, you can set a variable set to 1 outside your loop. In the loop, you add the "active" class when it's equal to 1, then you increment it.
<?php
// Previous code here...
$i = 1;
while ( $the_query->have_posts() ) :
$the_query->the_post();
?>
<div class="item<?php if ($i == 1) echo 'active'; ?>">
</div>
<?php
$i++;
endwhile;
wp_reset_postdata();
?>
This is the solution I came up with:
<?php
$args = array(
'post_type' => 'slides',
'oderby' => 'menu_order',
'posts_per_page' => -1
);
$slides = new WP_Query( $args );
if( $slides->have_posts() ): ?>
<div class="row">
<div class="col-xs-12">
<!--Twitter bootstrap Photo carrousel-->
<div id="myCarousel" class="carousel slide center-block" data-ride="carousel" >
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
<li data-target="#myCarousel" data-slide-to="3"></li>
<li data-target="#myCarousel" data-slide-to="4"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<?php while( $slides->have_posts() ) : $slides->the_post(); $index++ ?>
<?php if ( $index == 1 ): ?>
<div class="item active">
<?php else: ?>
<div class="item">
<?php endif; ?>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id() ); ?>
<img src="<?php echo $url; ?>" alt="<?php the_title(); ?>">
</div>
<?php endwhile; ?>
<?php endif; ?>
<!-- Left and right controls -->
<a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div><!-- carrousel ends here -->
I learnt this by watching the following video: Integrating the Bootstrap Carousel into the WordPress theme by user Ezer Sanbe. All credits to him.
The youtube video or the channel for this user are no longer available, sorry
Hope this helps
Limit the first query to 1 post. In that first loop, set the carousel item class to active. Reset the query and run a second loop, offset by one, and negate the active class, like so:
<div class="carousel-inner">
<?php
$the_query = new WP_Query(array(
'post_type' =>'post',
'posts_per_page' => 1
));
while ( $the_query->have_posts() ) :
$the_query->the_post();
?>
<div class="item active">
First Slide
</div>
<?php endwhile; wp_reset_postdata(); ?>
<?php
$the_query = new WP_Query(array(
'post_type' =>'post',
'offset' => 1
));
while ( $the_query->have_posts() ) :
$the_query->the_post();
?>
<div class="item">
Remaining Slides
</div>
<?php endwhile; wp_reset_postdata(); ?>
</div>
Bootstrap 3 with custom post type (here named "diapositives"):
<!-- Define the loop -->
<?php $diapositivesloop = new WP_Query( array( 'post_type' => 'diapositives', 'posts_per_page' => -1 ) ); ?>
<?php $i=1; ?>
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<?php while ( $diapositivesloop->have_posts() ) : $diapositivesloop->the_post(); ?>
<?php the_content(); ?>
<div class="item <?php if ($i == 1) echo 'active'; ?>">
<img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id() ); ?>" alt="...">
<div class="carousel-caption">
<h3><?php the_title(); ?></h3>
</div>
</div>
<!-- End of the loop -->
<?php $i++; ?>
<?php endwhile; wp_reset_query(); ?>
<!-- Controls -->
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
Integrating Bootstrap carousel in WordPress without plugin
<!-- Carousel items -->
<div class="carousel-inner">
<?php $slider = new WP_Query(array(
'post_type' => 'slider',
'posts_per_page' => 1,
)); ?>
<?php
if ( have_posts() ) {
$x=0;
while ( $slider->have_posts() ) {
$x++;
$slider->the_post(); ?>
<div class="<?php if($x==1){echo 'active'} ?> item">
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
</div>
<?php } // end while
} // end if
?>
</div>
<!-- Carousel nav -->
<ol class="carousel-indicators">
<?php for($i=0; $i<$x; $i++; ) { ?>
<li data-target="#carousel" data-slide-to="<?php echo $i; ?>" class="<?php if($i==0){ echo 'active' }?>"></li>
<?php } ?>
</ol>
<a class="carousel-control left" href="#carousel" data-slide="prev">‹</a>
<a class="carousel-control right" href="#carousel" data-slide="next">›</a>
</div>
<div id="carousel-1" class="carousel slide">
<!-- Wrapper for slides -->
<div class="carousel-inner">
<?php
$slider = new WP_Query(array(
'post_type' => 'slider',
'posts_per_page' => 7,
));
?>
<?php
if ( have_posts() ) {
$x=0;
while ( $slider->have_posts() ) {
$x++;
$slider->the_post(); ?>
<!-- Begin Slide 1 -->
<div class="item <?php if($x==1){ echo 'active'; }?>">
<?php if ( has_post_thumbnail() ) : ?>
<?php the_post_thumbnail('slider-img'); ?>
<?php endif; ?>
<div class="carousel-caption">
<h3 class="carousel-title hidden-xs"><?php the_title(); ?></h3>
<p class="carousel-body"><?php the_content(); ?></p>
</div>
</div>
<!-- End Slide 1 -->
<?php } // end while
wp_reset_postdata();
} // end if
?>
</div>
<!-- Indicators -->
<ol class="carousel-indicators visible-lg">
<?php
for($i=0; $i<$x; $i++){ ?>
<li data-target="#carousel-1" data-slide-to="<?php echo $i; ?>" class="<?php if( $i==0 ){echo 'active'; }?>"></li>
<?php }
?>
</ol>
<!-- Controls -->
<a class="left carousel-control" href="#carousel-1" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a class="right carousel-control" href="#carousel-1" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
</div>