简体   繁体   English

如何在wordpress中以自定义帖子类型循环进入单个附件帖子?

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

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 请帮助我解决这个烦人的问题,因为我是wordpress的大三学生,我的项目中有其他滑块,并且我创建了一个自定义帖子类型来制作附件帖子,以使每个帖子都成为一个滑块..问题是如何循环到CPT中以检索每条帖子,当我想要在前端显示时,我尝试了很多,并在网上搜索了有关它的信息,但是我发现并用它的唯一代码使每条帖子都覆盖了第一个帖子。这是我的代码

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

If you want to create a slider, set the featured image for every slider post and use that image in your WP_Query loop. 如果要创建滑块,请为每个滑块发布设置特色图片,并在WP_Query循环中使用该图片。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM