how to loop in single attachment posts in custom post type in wordpress?

1k Views Asked by At

please help me with this annoying issue as i'm junior in wordpress , i have a different slider in my project and i made a custom post type to make attachment posts on in to make every single post as a slider .. the problem is how to loop inside CPT to retrieve every single post when i want is to display on front-end i tried alot and searched on net about it but the only code that i found and i used it made every single post to override the first post .. this is my code

    <?php 
        $query = new WP_Query( array(

            'post_type'     =>  'owlgalleryslider',
            'posts_per_page'=>  3,
            'fields'        =>  'ids'
        ));

        $image_query = new WP_Query(array(

            'post_type' => 'attachment',
            'post_status' => 'puplish', 
            'post_mime_type' => 'image',
            'posts_per_page' => 3, 
            'post_parent' => $query->id, 
            'order' => 'DESC' 
        ));

    ?>

    <?php if( have_posts( )) : ?>

        <?php while ($image_query->have_posts()) : $image_query->the_post( ); ?>
            <div class="item owl-slide"> <img src="<?php echo wp_get_attachment_url( get_the_ID() ); ?>" /> </div>
        <?php endwhile;  wp_reset_query( );?>

    <? endif;?>
1

There are 1 best solutions below

1
On

If you want to create a slider, set the featured image for every slider post and use that image in your WP_Query loop.

     <?php 
             $query = new WP_Query( array(

            'post_type'     =>  'owlgalleryslider',
            'posts_per_page'=>  3,

        ));

     ?>

     <?php if( have_posts( )) : 
     while ($query->have_posts()) : $query->the_post(); 
     $image = wp_get_attachment_url( get_post_thumbnail_id() );
     ?>
     <div class="item owl-slide"> <img src="<?php echo $image; ?>" /> 
     </div>
     <?php endwhile;  wp_reset_query( );
     endif;
     ?>