久久久精品2019免费观看_亚洲国产精品成人久久久_69国产成人综合久久精品91_国产精品久久精品视

獲取WordPress文章中上傳的所有圖片

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文章中上傳的所有圖片

贊 (0) 打賞

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

微信掃一掃打賞