Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save PudparK/cfa216e6777904c587d2777e86f1e52e to your computer and use it in GitHub Desktop.
Save PudparK/cfa216e6777904c587d2777e86f1e52e to your computer and use it in GitHub Desktop.
Dynamic Bootstrap Carousel Custom Post Type Query in Shortcode
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