2012-01-20 15 views
0

我有以下代码:获取附着物只为这个职位

<?php 

    include('includes/index.php'); 
    get_header(); 

?> 
    <section> 
    <div class="text"> 
<?php 

    while (have_posts()): 
    the_post(); 
    if (get_the_title() == 'Archive') query_posts('posts_per_page=1&cat=1'); 
    $category = get_the_category(); 
    if ($category[0]->name) echo '<h1>'.$category[0]->name.'</h1>'; 
    $attachments = get_children(array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' =>'image')); 
    foreach ($attachments as $attachment_id => $attachment): 
     $image = wp_get_attachment_url($attachment_id, 'medium'); 
     echo $image."<br />"; 
    endforeach; 
    the_content(); 
    endwhile; 

?> 
<?php edit_post_link('Click here in order to edit this page'); ?> 
    </div><!--END /.text--> 
    </section> 
</div><!-- end clearfix--> 
<?php get_footer(); ?> 

此代码是在page.php其中默认我列出post.I想要得到的附着物仅针对后query_posts('posts_per_page=1&cat=1');现在它也得到该页面的附件。,我该如何做到这一点?

+0

通过告诉query_posts(或更好的自定义查询对象),你会喜欢的一个特定的职位。请参阅http://codex.wordpress.org/Function_Reference/WP_Query – hakre

回答

0

下面是一个用于演示目的的简单示例。它显示的get_posts使用(类似于query_posts)来检索当前职位的所有附件:

<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> 

来源:Codex: Display Images as a list