如何获取Wordpress类别或slu下的文件列表

时间:2012-03-19 10:57:23

标签: php 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>

1 个答案:

答案 0 :(得分: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();

?>
相关问题