first post in while loop of wordpress - php

I am trying to create a bootstrap carousel in WordPress using advanced custom fields.
The first 'carousel-item' in the loop must have class 'active'. I am not able to figure out how to define the if condition so that the class is added to the first iteration of the loop.
Same is the case with the carousel indicators.
class active should be added to the first iteration and data-slide-to="x" should be the counter of the loop. Any idea how to get the count and class working?
<section>
<div id="theCarousel" class="carousel slide" data-ride="carousel" data-interval="false">
<ol class="carousel-indicators">
<li data-target="#theCarousel" data-slide-to="0" class="active"></li>
<li data-target="#theCarousel" data-slide-to="1"></li>
<li data-target="#theCarousel" data-slide-to="2"></li>
</ol>
<div class="carousel-inner" role="listbox">
<?php $loop = new WP_Query( array ( 'post_type' => 'carousel', 'orderby' => 'post_id', 'order' => 'ASC') ); ?>
<?php while( $loop->have_posts() ): $loop->the_post(); ?>
<div class="carousel-item" style="background-image: url('<?php the_field('carousel_image'); ?>')">
<div class="carousel-caption d-none d-md-block">
<h3><?php the_title(); ?></h3>
<p><?php the_field('carousel_description'); ?></p>
</div>
</div>
<?php endwhile; wp_reset_query();?>
</div>
<a class="carousel-control-prev" href="#theCarousel" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#theCarousel" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</section>

You can do it this way
<div class="carousel-inner" role="listbox">
<?php
$iteration = 0;
$loop = new WP_Query( array ( 'post_type' => 'carousel', 'orderby' => 'post_id', 'order' => 'ASC') ); ?>
<?php while( $loop->have_posts() ): $loop->the_post(); $iteration++; ?>
<div class="carousel-item<?php if( $iteration == 1 ) echo ' active' ?>" style="background-image: url('<?php the_field('carousel_image'); ?>')" data-slide-to="<?php echo $iteration ?>">
<div class="carousel-caption d-none d-md-block">
<h3><?php the_title(); ?></h3>
<p><?php the_field('carousel_description'); ?></p>
</div>
</div>
<?php endwhile; wp_reset_query();?>
</div>
<a class="carousel-control-prev" href="#theCarousel" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#theCarousel" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</section>

Here's how I implemented it.
Defined two variables. One for the carousel indicator, and one for the carousel wrapper and increment them in the loop.
$carousel_wrap = 0;
$carousel_ind = 0;
<section>
<div id="theCarousel" class="carousel slide" data-ride="carousel" data-interval="false">
<ol class="carousel-indicators">
<?php $loop = new WP_Query( array ( 'post_type' => 'carousel', 'orderby' => 'post_id', 'order' => 'ASC') ); ?>
<?php while( $loop->have_posts() ): $loop->the_post(); $carousel_ind++; ?>
<li data-target="#theCarousel" data-slide-to="<?php echo $carousel_ind; ?>" class="<?php if( $carousel_ind == 1 ) echo 'active' ?>""></li>
<?php endwhile; wp_reset_query();?>
</ol>
<div class="carousel-inner" role="listbox">
<?php $loop = new WP_Query( array ( 'post_type' => 'carousel', 'orderby' => 'post_id', 'order' => 'ASC') ); ?>
<?php while( $loop->have_posts() ): $loop->the_post(); $carousel_wrap++; ?>
<div class="carousel-item <?php if( $carousel_wrap == 1 ) echo 'active' ?>" style="background-image: url('<?php the_field('carousel_image'); ?>')">
<div class="carousel-caption d-none d-md-block">
<h3><?php the_title(); ?></h3>
<p><?php the_field('carousel_description'); ?></p>
</div>
</div>
<?php endwhile; wp_reset_query();?>
</div>
<a class="carousel-control-prev" href="#theCarousel" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#theCarousel" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</section>

Use for loop for that.
for ( $iteration = 0; $the_query->have_posts(); $iteration++ ) : $the_query->the_post();
endfor;

Related

Integrating Bootstrap Carousel Indicators with ACF Plugin

I am using the advanced custom fields Wordpress plugin to create various slides for a slider. To display the slider I am using Bootstraps Carousel.
The body of the slider if functioning fine. I don't, however, know how to loop through, count the slides and print a carousel indicator to the page for each slide.
I currently have 3 hardcoded at the top of the slider.
<ul id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
</ol>
<li class="carousel-inner">
<?php
$c = 0;
$class = '';
while ( have_rows('slide') ) : the_row();
$c++;
if ( $c == 1 ){ $class = ' active';}
else{ $class=''; } ?>
<?php
$image = get_sub_field('image'); ?>
<div class="carousel-item <?php echo $class; ?> image" style="background: url('<?php echo $image; ?>') no-repeat; background-size: cover; background-position: left center;">
</div>
<?php
endwhile; ?>
</li> <!-- end li.image -->
</ul> <!-- end ul -->
I need to find a way to open the ordered list before the slider starts and close it when it ends. At the same time, I need to echo out its li elements for each slide.
<section id="banner">
<?php if( have_rows('slides') ) { ?>
<?php
$num = 0;
$active = 'active';
?>
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<?php while( have_rows('slides') ) : the_row() ; ?>
<li data-target="#carouselExampleIndicators" data-slide-to="<?php echo $num; ?>" class="<?php echo $active; ?>"></li>
<?php
$num++;
$active = '';
?>
<?php endwhile; ?>
</ol>
<div class="carousel-inner">
<?php $active = 'active'; ?>
<?php while( have_rows('slides') ) : the_row() ;
$image = get_sub_field('image');
$mainText = get_sub_field('main_text');
$subText = get_sub_field('sub_text');
?>
<div class="carousel-item <?php echo $active; ?>">
<img class="d-block w-100" src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>">
<div class="carousel-caption d-none d-md-block">
<h5><?php echo $mainText; ?></h5>
<p><?php echo $subText; ?></p>
</div>
</div>
<?php $active = ''; ?>
<?php endwhile; ?>
</div>
<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
<?php } ?>
</section>
If your HTML is like this :
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
</ol>
<div class="carousel-inner" role="listbox">
<div class="carousel-item active">
<img class="d-block img-fluid" src="..." alt="First slide">
</div>
<div class="carousel-item">
<img class="d-block img-fluid" src="..." alt="Second slide">
</div>
<div class="carousel-item">
<img class="d-block img-fluid" src="..." alt="Third slide">
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
Then use this php code :
<?php
$sliders = get_field('slide');
if($sliders){ ?>
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<?php $isActive ='';
foreach($sliders as $key=>$slider){
if($key==0){
$isActive = 'active';
}
echo '<li data-target="#carouselExampleIndicators" data-slide-to="'.$key.'" class="'.$isActive.'"></li>';
} ?>
</ol>
<div class="carousel-inner" role="listbox">
<?php
$activeSlide ='';
foreach($sliders as $key=>$sliderimg){
if($key==0){
$activeSlide = 'active';
}
echo '<div class="carousel-item '.$activeSlide.'">';
echo '<img class="d-block img-fluid" src="'.$sliderimg['image']." alt="First slide">';
echo '</div>';
?>
</div>
<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
<?php } ?>

Bootstrap carousel for dynamic content in Wordpress

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;?>
`

wordpress PHP to show 3 slide per page with bootstrap carousel

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>

How to get images dynamically From wordpress to image slide?

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>

Integrating Bootstrap carousel in WordPress without plugin

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>

Categories