Wordpress将所有图像保存为帖子类型,称为附件。您需要运行查询来检索这些附件。您还需要指定要检索的附件类型,因为并非所有附件都是图像。视频、pdf文件和音频文件也是附件,因此您需要将mime类型指定为图像。以下是检索所有图像的查询
<?php
$new_query = new WP_Query(array(
\'posts_per_page\' => 1
));
while ($new_query->have_posts()) : $new_query->the_post();
$args = array (
\'post_type\' => \'attachment\',
\'numberposts\' => -1,
\'status\' => \'publish\',
\'post_mime_type\' => \'image\',
\'parent\' => $post->ID
);
$attachments = get_posts($args);
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo \'<div class="oneCell">\';
echo \'<a href="\' . get_permalink( $post->ID ) . \'">\';
echo wp_get_attachment_image( $attachment->ID, \'full\' );
echo \'</a>\';
echo \'</div>\';
}
}
endwhile;
wp_reset_postdata();
?>
进一步阅读并了解
WP_Query
, 查看
codex