Forked from azizultex/Dynamic Bootstrap Carousel Custom Post Type Query in Shortcode
Created
November 21, 2018 05:02
-
-
Save PudparK/cfa216e6777904c587d2777e86f1e52e to your computer and use it in GitHub Desktop.
Dynamic Bootstrap Carousel Custom Post Type Query in Shortcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function slider_shortcode() { | |
$args = array( 'post_type' => 'slider', 'posts_per_page' => 4 ); | |
$loop = new WP_Query( $args ); | |
$return = '<div id="carousel-example-generic" class="carousel slide carousel-fade" data-ride="carousel"> | |
<ol class="carousel-indicators">'; | |
while ( $loop->have_posts() ) : $loop->the_post(); | |
$numbers = $loop->current_post; | |
$active = ( $loop->current_post == 0 ) ? 'class="active"' : ''; | |
$return .= '<li data-target="#carousel-example-generic" data-slide-to="'.$numbers.'" '.$active.'></li>'; | |
endwhile; | |
$return .= '</ol><div class="carousel-inner">'; | |
while ( $loop->have_posts() ) : $loop->the_post(); | |
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); | |
$active = ( $loop->current_post == 0 ) ? 'active' : ''; | |
$return .= '<div class="item '.$active.'"> | |
<img src="'.$image[0].'" alt="" /> | |
<div class="container"> | |
<div class="carousel-caption"> | |
'.get_the_content().' | |
</div> | |
</div></div>'; | |
endwhile; | |
$return .= '</div></div>'; | |
return $return; | |
} | |
add_shortcode('slider', 'slider_shortcode'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment