Wordpress:查询帖子媒体库中的所有图像

sup*_*led 8 wordpress wordpress-theming

你好!我正在寻找一种方法来列出帖子的媒体库中的所有图像文件.

我的意思是,如果在创建或编辑帖子时上传了文件,是以某种方式与帖子关联的文件,我可以从这些数据创建列表.

我认为next_image_link()/ previous_image_link(); 模板标签就像我发现的那样近.

我认为这应该是接近的:

$query = 'SELECT * FROM `wp_posts` 
WHERE `post_parent` = \''.$_GET['post_id'].'\' 
AND  `post_mime_type` = \'image/jpeg\' 
ORDER BY `menu_order` ASC';
Run Code Online (Sandbox Code Playgroud)

谢谢.

ari*_*ayu 11

在wordpress术语中,您上传到特定帖子的每个图像都称为附件.要列出所有附件,可以使用get_children()函数:

$images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=10' );

$counter=0;
foreach( (array) $images as $attachment_id => $attachment )
{
   $counter++;
   echo "<a href='".wp_get_attachment_link( $attachment_id ) . "'>image $counter</a><br />";
}
Run Code Online (Sandbox Code Playgroud)

算法是这样的.