投稿画像を取得するには

記事に投稿されている画像を取得するには、添付ファイルを取得するget_children()でimageを指定する。画像のURLのほか、タイトルやサイズなどのメタ情報も取得することもできる。

$attachments = get_children(array('post_parent' => $id, 'post_type' => 'attachment', 'post_mime_type' => 'image'));
if (is_array($attachments) ){
	foreach($attachments as $attachment){
		$image = wp_get_attachment_url(intval($attachment->ID));
		echo '<img src="' .$image .'" alt="' .$post->post_title .'" />';
	}
}

コメントを残す

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>