简体   繁体   English

如何在媒体库中循环显示图像? (wordpress)

[英]How to loop over images in media library and display them? (wordpress)

I'm trying to make a simple gallery in wordpress (I've tried out some plugins but none of them convince me). 我正在尝试在wordpress中建立一个简单的库(我尝试了一些插件,但没有一个使我信服)。 The thing is that the solution I find more useful is to loop over the images I have already uploaded to the media library and display them (as a grid gallery). 问题是,我发现更有用的解决方案是遍历已上传到媒体库的图像并显示它们(作为网格库)。

The problem is that I don't find any information about how to loop over the media library images and display them as thumbnails, any suggestions? 问题是我找不到有关如何遍历媒体库图像并将其显示为缩略图的任何信息,有什么建议吗?

Something like this? 像这样吗

$args = array(
        'post_type' => 'attachment',
        'post_mime_type' => 'image',
        'orderby' => 'post_date',
        'order' => 'desc',
        'posts_per_page' => '30',
        'post_status'    => 'inherit'
         );

     $loop = new WP_Query( $args );

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

$image = wp_get_attachment_image_src( get_the_ID() ); 
echo "<img src='" . $image[0] . "'>";

endwhile;

You can try this: 您可以尝试以下方法:

<section class="row align-middle">
        <?php 
        $query_images_args = array(
            'post_type'      => 'attachment',
            'post_mime_type' => 'image,video',// video files include
            'post_status'    => 'inherit',
            'orderby'        => 'post_date',
            'posts_per_page' =>  30,
        );

        $query_images = new WP_Query( $query_images_args );

        if($query_images->have_posts()) : 
            while($query_images->have_posts()) : 
                $query_images->the_post(); ?>

                <div class="small-6 medium-4 large-2 columns">
                    <?php echo $images = wp_get_attachment_image( $query_images->posts->ID, 'thumbnail' ); ?>
                </div>

            <?php endwhile; ?>
        <?php else : ?>
            <p>No media file yet</p>
        <?php endif;

        /* Restore original Post Data */
        wp_reset_postdata(); ?>
</section>

Remember restore original Post Data to avoid issues with others querys in the same page. 请记住恢复原始的发布数据,以避免同一页面中其他查询出现问题。

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

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