当你是一个网页或职位上,你可以得到所有的附件有以下:
global $post; // refers to the post or parent being displayed
$attachements = query_posts(
array(
'post_type' => 'attachment', // only get "attachment" type posts
'post_parent' => $post->ID, // only get attachments for current post/page
'posts_per_page' => -1 // get all attachments
)
);
foreach($attachements as $attachment){
// Do something exceedingly fancy
}
由于您目前所在的附件页面上,你可以用得到的所有其他附件所述$post->post_parent
值:
global $post; // refers to the attachement object
$attachements = query_posts(
array (
'post_type' => 'attachment', // only get "attachment" type posts
'post_parent' => $post->post_parent, // attachments on the same page or post
'posts_per_page' => -1 // get all attachments
)
);
要然后显示该附件的图像,则可以使用wp_get_attachment_image_src功能。附件的ID将在您的foreach循环的每次迭代中都可用,如$attachement->ID
(如果您使用与第一个示例相同的命名约定)。
来源
2012-02-27 23:35:42
Pat
简单的光荣! – oles 2014-10-09 09:29:09