Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Display Custom Post Type's Gallery (images )Through WP_Query WordPress

I am trying to load an Image Slider images from a Custom Post Type image Gallery Feature. For example let say I have an Custom Post Type called banner which I can load images to it's Gallery and I have an HTML code like this:

<div class="item active">
<img src="assets/img/ban1r.jpg" alt="">
<div class="carousel-caption">
slide 1
</div>
</div>

<div class="item active">
<img src="assets/img/ban2r.jpg" alt="">
<div class="carousel-caption">
slide 1
</div>
</div>

Can you please let me know how I can use the custom WP_Query to retrive the images from the custom post and load them into slider instead of above HTML?

So far I have done something like this but it is not displaying any image!

<?php
$args = array( 'post_type' => 'banner');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$gallery = get_post_gallery_images($post);
foreach( $gallery as $image ) {
echo '<div class="item active">';
echo '<img src="' . $image . '">';
echo '</div>';
}
endwhile;
?>


* - Please be advised that the first item must be in active class

Answers (1)

2013-11-07

Arnav Joy answers:

try this

<?php

$args = array( 'post_type' => 'banner');

$loop = new WP_Query( $args );

while ( $loop->have_posts() ) : $loop->the_post();

$gallery = get_posts( array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_parent' => get_the_ID()

) );


if( !empty( $gallery ) ) {
$i=1;
foreach( $gallery as $image ) {


$large_image_url = wp_get_attachment_image_src( $image->ID , 'large');

if( $i==1 )
echo '<div class="item active">';
else
echo '<div class="item">';

echo '<img src="' . $large_image_url[0] . '">';

echo '</div>';

$i++; }
}
endwhile;