2010-07-22 32 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

相关问题