2012-03-19 35 views
0

我有一個類別,它有這些類別下的子類別我有作爲附件發佈。我想只檢索這個類別的文件,爲此我使用下面的代碼,但它不工作?如何獲取Wordpress類別或子彈下的文件列表

<ul> 
    <?php 
    global $wpdb; 
    $max_posts = 5; 
    $sql = "SELECT posts.ID, attach.ID attachID, attach.post_title, MIN(attach.post_date) 
    FROM $wpdb->posts posts, $wpdb->posts attach 
    WHERE posts.ID = attach.post_parent 
    AND attach.post_type='attachment' 
    AND attach.post_status = 'inherit' 
    AND posts.post_type = 'post' 
    AND posts.post_status = 'publish' 
    AND posts.post_date <= NOW() 
    GROUP BY posts.ID 
    ORDER BY posts.post_date DESC 
    LIMIT $max_posts"; 

    $postIDs = $wpdb->get_results($sql); 
    foreach ($postIDs as $postID) { 
    the_attachment_link($postID->attachID,false); 


    } 
    ?> 
    </ul> 

回答

0

我認爲你應該使用這個

<?php 

// The Query 
$the_query = new WP_Query('cat=13&post_type=publication&numberposts=5'); 

// The Loop 

    while ($the_query->have_posts()) : $the_query->the_post(); 

      echo '<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>'; 


    endwhile; 

// Reset Post Data 
wp_reset_postdata(); 

?> 
相關問題