我试图为我的网站制作一个滑块,但我现在有点卡住了。问题是我不想使用任何插件(我更喜欢尝试学习如何做到这一点,而且大多数插件都有我不需要的额外功能),我认为的解决方案是检索我上传到网站的所有图片。
我已经知道如何做到这一点,因为我在其他网站上做过,但问题出现在我上传的图片中我不希望它们显示在网页上。这有什么解决方案吗?有点像只检索具有相同特定值的图像......
提前致谢!
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>
答案 0 :(得分:0)
在Wordpress中,附件只是另一种post_type - 就像你的查询建议的那样。也就是说,您可以通过几种方式区分上传内容。但是,WP Admin并没有为您提供任何现成的方法。您必须深入了解并滚动自己的界面进行图片上传,或使用Settings API将元框添加到附件页面。
一个。保存或更新图像时设置元键/值组合:
add_post_meta($post->id, $meta_key, $meta_value);
湾按元键/值
搜索$args = [ 'post_type' => 'attachment', 'meta_key' => $meta_key, 'meta_value' => $meta_value ]; $attachments = get_posts($args);
一个。按照此处的说明操作:
http://codex.wordpress.org/Post_Types#Custom_Post_Types
湾按帖子类型搜索。
$attachments = get_posts(['post_type' => $custom_post_type_handle]);