我想获取161到166个图像,而无需调用整个媒体库阵列并拼接它们。随着时间的推移,我在那里的信息越多,我的网站速度就会越慢。这是我到目前为止所拥有的,我使用array\\u reverse来反转ID,以便最近的上载是最后一次,我使用array\\u splice来查找需要提取的图像。有没有更直接的方法让我找到ID为161到166的图像?
function get_images_from_media_library() {
$args = array(
\'post_type\' => \'attachment\',
\'post_mime_type\' =>\'image\',
\'post_status\' => \'inherit\',
\'posts_per_page\' => -1,
);
$query_images = new WP_Query( $args );
$images = array();
foreach ( $query_images->posts as $image) {
$images[]= $image->guid;
}
$images = array_reverse($images);
$images = array_splice($images, 3,6);
return $images;
}
$img = get_images_from_media_library();
foreach($img as $image){
echo "<img src=\'$image\'/>";
}
最合适的回答,由SO网友:Pat J 整理而成
添加post__in
(两条下划线)到您的$args
:
$args = array(
\'post_type\' => \'attachment\',
\'post_mime_type\' =>\'image\',
\'post_status\' => \'inherit\',
\'posts_per_page\' => -1,
\'post__in\' => array( 161, 162, 163, 164, 165, 166 ),
);
参考
WP_Query on the Codex