WordPress的圖片如果是在發(fā)布、編輯時上傳的,那么是有一個隸屬于關(guān)系,即:圖片是隸屬于這一篇文章。我們可以通過這種隸屬關(guān)系將文章中的所有圖片找出來。
在single.php頁面的the_loop代碼片段中加入:
<?php
$args = array(
'post_type' => 'attachment', // 屬于附件類型
'numberposts' => -1, // 查出所有內(nèi)容
'post_status' => null, // 發(fā)布狀態(tài)
'post_mime_type' => 'image', // 附件類型為圖片
'post_parent' => $post->ID // 隸屬于這篇文章
);
$attachments = get_posts( $args );
// 如果存在
if ( $attachments ) {
// 循環(huán)輸出
foreach ( $attachments as $attachment ) {
var_dump($attachment);
}
}?>
下圖就是循環(huán)輸出文章中的圖片截圖:
未經(jīng)允許不得轉(zhuǎn)載:445IT之家 » 獲取WordPress文章中上傳的所有圖片