Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save azizultex/b46889014400fe4a7132 to your computer and use it in GitHub Desktop.
Save azizultex/b46889014400fe4a7132 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');
@suprimgod
Copy link

how to use this code on backend?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment