如何只使用query_posts获取父页面的附件?

时间:2012-06-28 17:24:34

标签: php wordpress templates

基本上我正在尝试获取父页面的图库,我一直在使用query_posts来做到这一点,以下代码似乎让我更接近我需要但实际上是从其他地方获取附件而不是只有当前页面的父页面,任何一个?:

<?php
 $args = array('post_type' => 'attachment',
  'numberposts' => -1,
  'post_status' => null,
  'post_parent' => 0,
  'order_by' => 'menu_order',
  'order' => 'ASC');
 $attachments = get_posts($args);
 if($attachments)
 {
  echo '<ul class="imagelist">';
  foreach($attachments as $attachment)
 {
 echo '<li>';
$large = wp_get_attachment_image_src($attachment->ID, 'large');
$thumb = wp_get_attachment_image($attachment->ID, 'thumbnail');
echo '<a href="'. $large[0] .'">' . $thumb . '</a>';
echo '</li>';
 }
 echo '</ul>';
}
?>

1 个答案:

答案 0 :(得分:1)

您应该将post_parent设置为当前帖子父ID:

global $post;
$args = array(
    'post_type' => 'attachment',
    'numberposts' => -1,
    'post_status' => null,
    'post_parent' => $post->post_parent,
    'order_by' => 'menu_order',
    'order' => 'ASC'
);