2010-07-22 64 views

回答

23

上傳圖像被存儲爲與類型「附件」的帖子;使用get_posts()和正確的參數。在the Codex entry for get_posts(),這個例子:

<?php 

$args = array(
    'post_type' => 'attachment', 
    'numberposts' => -1, 
    'post_status' => null, 
    'post_parent' => null, // any parent 
    ); 
$attachments = get_posts($args); 
if ($attachments) { 
    foreach ($attachments as $post) { 
     setup_postdata($post); 
     the_title(); 
     the_attachment_link($post->ID, false); 
     the_excerpt(); 
    } 
} 

?> 

...遍歷所有的附件,並顯示它們。

如果您只想獲取圖像,TheDeadMedic評論過,您可以在參數中使用'post_mime_type' => 'image'進行過濾。

+2

是的,你可以用' 'post_mime_type'=>「image''在'$ args',和WordPress會巧妙地搭配,對所有的圖像MIME類型:) – TheDeadMedic 2010-07-22 11:31:15

+1

@TheDeadMedic好消息!我從來沒有需要回到特定的類型。 – 2010-07-22 12:05:41

+0

對於匹配MIME類型的TheDeadMedic很好的答案和榮譽! :) – hsatterwhite 2010-07-22 12:17:13

1
<ul> 
      <?php if (have_posts()) : while (have_posts()) : the_post();  

        $args = array(
         'post_type' => 'attachment', 
         'numberposts' => -1, 
         'post_status' => null, 
         'post_parent' => $post->ID 
         ); 

        $attachments = get_posts($args); 
        if ($attachments) { 
         foreach ($attachments as $attachment) { 
          echo '<li>'; 
          echo wp_get_attachment_image($attachment->ID, 'full'); 
          echo '<p>'; 
          echo apply_filters('the_title', $attachment->post_title); 
          echo '</p></li>'; 
         } 
        } 

      endwhile; endif; ?> 
     </ul> 
+0

我是否需要創建一個新頁面? – TheBlackBenzKid 2016-09-19 05:44:29

相關問題