显示具有相同特定值的图像? (WordPress)

Display images with same specific value? (Wordpress)

我正在尝试为我的网站制作一个滑块,但我现在有点卡住了。问题是我不想使用任何插件(我更愿意尝试学习如何做到这一点,而且大多数插件都有我不需要的额外功能),我认为的解决方案是检索我的所有图像已上传到网站。

我已经知道怎么做了,因为我是在另一个网站上做的,但是当我上传我不希望它们显示在网页上的图片时,问题就来了。有什么解决办法吗?比如只检索那些具有相同特定值的图像...

提前致谢!

PD:这是我用来检索所有上传图片的代码:

<ul id="gallery_list">
                    <?php
                        $args = array(
                            'post_type' => 'attachment',
                            'post_mime_type' => 'image',
                            'orderby' => 'post_date',
                            'order' => 'desc',
                            'posts_per_page' => '10',
                            'post_status'    => 'inherit'
                        );

                        $loop = new WP_Query( $args );

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

                        $image = wp_get_attachment_image_src( get_the_ID() );
                        $image_url = wp_get_attachment_url( get_the_ID() );

                        echo "<li><img src='" . $image[0] . "' alt=''></li>";

                        endwhile;
                    ?>
</ul>

在 Wordpress 中,附件只是另一种 post_type--just,就像您的查询所建议的那样。也就是说,您可以通过几种方法来区分您上传的内容。但是,WP Admin 不会为您提供任何现成的方法来执行此操作。您将不得不深入了解并使用您自己的图像上传界面或使用设置 API 将元框添加到附件页面。

  1. 使用wp_post_meta:

a. Set a meta key/value combination when saving or updating the image:

add_post_meta($post->id, $meta_key, $meta_value);

b. Search by meta key/value

$args = [      'post_type' => 'attachment',    'meta_key' => $meta_key,
 'meta_value' => $meta_value
];

$attachments = get_posts($args);
  1. 创建你自己的Post类型:

a. Follow the instructions here:

http://codex.wordpress.org/Post_Types#Custom_Post_Types

b. Search by post type.

$attachments = get_posts(['post_type' => $custom_post_type_handle]);