WP_Query按类别ID获取帖子可返回所有类别

时间:2019-10-05 08:29:00

标签: php wordpress

我正在尝试编写一个WP_Query来检索某个类别中的所有帖子,并将这些帖子显示在博客页面上。

<?php
    $args  = array(
        'post_status' => 'publish',
        'cat' => 24,
        'order' => 'DESC'
    );
    $query = new WP_Query($args);
    while ( $query->have_posts() ):
        $query->the_post();
        get_template_part( 'templates/content/content', 'loop' );
    endwhile;
?>

尽管查询仅按降序返回已发布的帖子,但category参数被忽略(它返回所有类别的内容循环模板,而不仅仅是类别24)。

这是管理面板中的类别URL:http://www.apokalipsa.si/wp-admin/term.php?taxonomy=category&tag_ID=24&post_type=post&wp_http_referer=%2Fwp-admin%2Fedit-tags.php%3Ftaxonomy%3Dcategory

我尝试将'cat' => 24,的查询参数与'tag_ID' => 24,交换(来自类别页面中的类别URL),但是结果是相同的。

1 个答案:

答案 0 :(得分:1)

    <?php
    $args  = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'category__in' => 24,
        'order' => 'DESC'
    );
    $query = new WP_Query($args);


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

        get_template_part('templates/content/content', 'loop');
    endwhile;

您可以尝试