2012-02-27 63 views
0

所以在我的博客中,我有一个照片附件页面,但它一次只显示照片,这两张照片被用作导航,我讨厌。如何显示当前帖子的wordpress附件?

我想要附件页面显示所有与该集合其余部分一起的照片。

下面是当前的代码

 <div id="nav-images" class="navigation clearfix"> 
      <div class="nav-next"><?php next_image_link() ?></div> 
      <div class="nav-previous"><?php previous_image_link() ?></div> 

我如何改变来显示所有的张贴?

回答

3

当你是一个网页或职位上,你可以得到所有的附件有以下:

global $post; // refers to the post or parent being displayed 
$attachements = query_posts(
    array(
    'post_type' => 'attachment', // only get "attachment" type posts 
    'post_parent' => $post->ID, // only get attachments for current post/page 
    'posts_per_page' => -1  // get all attachments 
) 
); 
foreach($attachements as $attachment){ 
    // Do something exceedingly fancy 
} 

由于您目前所在的附件页面上,你可以用得到的所有其他附件所述$post->post_parent值:

global $post; // refers to the attachement object 
$attachements = query_posts(
    array (
    'post_type' => 'attachment', // only get "attachment" type posts 
    'post_parent' => $post->post_parent, // attachments on the same page or post 
    'posts_per_page' => -1  // get all attachments 
) 
); 

要然后显示该附件的图像,则可以使用wp_get_attachment_image_src功能。附件的ID将在您的foreach循环的每次迭代中都可用,如$attachement->ID(如果您使用与第一个示例相同的命名约定)。

5

为了澄清,这不再起作用 - 至少在版本3.5.2。我用这个来代替;

$attachments = get_children(
    array(
    'post_type' => 'attachment', 
    'post_parent' => get_the_ID() 
) 
); 
foreach ($attachments as $attachment) { 
    // ... 
} 

只复活旧线程,因为此线程对此搜索字词的排名非常高。

+0

简单的光荣! – oles 2014-10-09 09:29:09

0

由于WordPress 3.6.0,你也可以使用get_attached_media

$media = get_attached_media('image', $post->ID); 
if(! empty($media)){ 
    foreach($media as $media_id => $media_file){ 
     $thumbnail = wp_get_attachment_image_src ($media_id, 'thumbnail'); 
     $full = wp_get_attachment_url($media_id); 
     echo '<a href="'.$full.'" target="_blank"><img src="'.$thumbnail[0].'" alt="'.$media_file->post_title.'" /></a>'; 
    } 
} 
相关问题