2013-05-06 67 views
0

我想制作一个自定义的Wordpress模板/页面,其中我列出了几个子类别中帖子所附的PDF。 我发现了几个片段,但我无法让他们工作。 我的PHP技能有限。列出附加的PDF和帖子中的类别。 WordPress的

这给我所有附加的PDF,但我想要4个不同的列表,每个类别一个。

<?php 
    $args = array(
    'post_type' => 'attachment', 
    'numberposts' => -1, 
    'post_status' => null, 
    'post_mime_type' => 'application/pdf', 
    'post_parent' => null, 
    ); 
    $attachments = get_posts($args); 
    if ($attachments) { 
     foreach ($attachments as $post) { 
      setup_postdata($post); 
    ?> 
     <ul class=""> 
      <li class=""><h2>Sub-Category-Name</h2> 
       <ul> 
       <li> 
       <?php 
       the_attachment_link($post->ID, false); 
       echo '</li>'; 
       } 
       } 
       ?> 
+0

请阅读[这里](http://wordpress.org/support/topic/pulling-attachments-from-specific-post-categories)。 – montrealist 2013-05-06 15:10:51

回答

0

我已经设法在一定程度上解决它。现在我在页面上运行这四次,每个循环中都有不同的类别名称。

   <ul> 
       <?php query_posts('category_name=your-category-name &posts_per_page=-1'); ?> 
        <?php while (have_posts()) : the_post(); ?> 
         <?php 
          $args = array(
           'order'   => 'ASC', 
           'post_type'  => 'attachment', 
           'post_parent' => $post->ID, 
           'post_mime_type' => 'application/pdf', 
           'post_status' => null, 
           'numberposts' => 1, 
           ); 
           $attachments = get_posts($args); 
           if ($attachments) { 
            foreach ($attachments as $attachment) { 
             echo "<li><a href='"; 
             echo the_permalink(); 
             echo "' title='"; 
             echo the_title(); 
             echo "'>"; 
             echo the_attachment_link($attachment->ID); 
             echo "</a></li>"; 
             } 
            } 
            ?> 
        <?php endwhile;?> 
       </ul> 
       <?php wp_reset_query(); ?>