How to Display Custom Post Type's Gallery (images ) in Through WP_Query

喜欢而已 提交于 2019-12-11 20:21:31

问题


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;
?>

回答1:


I tried something like this before 2 months ago, and working great for me.

global $wpdb;

$query = "select * from $wpdb->posts 
             where 
             post_type = 'banner' and 
             post_status = 'publish'";
$results = $wpdb->get_results($query, ARRAY_A);

foreach( $results as $result ){

    echo '<div id="'.$result['ID'].'" class="listen">';
    echo get_the_post_thumbnail( $result['ID'], array(200, 200) );
    </div>';

}

Take a look on get_the_post_thumbnail



来源:https://stackoverflow.com/questions/19722768/how-to-display-custom-post-types-gallery-images-in-through-wp-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!