模板中不需要SQL查询。
function wpse_72594_get_attachments( $id, $mime = \'\' )
{
$args = array(
\'post_type\' => \'attachment\',
\'post_mime_type\' => $mime,
\'post_parent\' => $id
);
$attachments = get_posts($args);
if ($attachments)
return $attachments;
return false;
}
然后调用如下函数(300是post ID):
wpse_72594_get_attachments(300)
, 抓取所有附件wpse_72594_get_attachments(300, \'image\' )
, 仅图像wpse_72594_get_attachments(300, \'application/pdf\' )
, 仅pdf文件