So I'm creating a shortcode that uses the standard bootstrap carousel.
It works completely fine as long as all the 3 slide attributes are entered correctly but obviously if only 2 are set and the forth is not it still creates the 3th div for the slide but just does not enter the image src url and therefore creates a blank slide. The thing is each time it will be used it will have a different amount of slides and I want it to work if only 2 image urls are passed or 4.
functions:
// Add Shortcode for single portfolio slider
function slider_shortcode( $atts , $content = null ) {
// Attributes
extract( shortcode_atts(
array(
'slide1' => '',
'caption1' => '',
'slide2' => '',
'caption2' => '',
'slide3' => '',
'caption3' => '',
), $atts )
);
// Code
return '
<div class="col-lg-7">
<div class="carousel slide padtop" data-ride="carousel" id=
"carousel-example-generic">
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<div class="item active">
<img src="' . $slide1 . '" class="img-responsive">
<div class="carousel-caption">
' . $caption1 . '
</div>
</div>
<div class="item">
<img src="' . $slide2 . '" class="img-responsive">
<div class="carousel-caption">
' . $caption2 . '
</div>
</div>
<div class="item">
<img src="' . $slide3 . '" class="img-responsive">
<div class="carousel-caption">
' . $caption3 . '
</div>
</div>
<div class="item">
<img src="' . $slide4 . '" class="img-responsive">
<div class="carousel-caption">
' . $caption4 . '
</div>
</div>
</div><!-- Controls -->
<a class="left carousel-control top20 transitionfast" data-slide=
"prev" href="#carousel-example-generic" role=
"button"><span aria-hidden="true" class=
"glyphicon glyphicon-chevron-left"></span> <span class=
"sr-only">Previous</span></a> <a class=
"right carousel-control top20 transitionfast" data-slide="next"
href="#carousel-example-generic" role="button"><span aria-hidden=
"true" class="glyphicon glyphicon-chevron-right"></span>
<span class="sr-only">Next</span></a>
</div>
</div>
';
}
add_shortcode( 'slider', 'slider_shortcode' );
Shortcode:
[slider slide1="/img/img1.png" caption1="" slide2="/img/img2.jpg" caption2="test text 2"][/slider]