簡體   English   中英

WP_Query get post by category id 返回所有類別

[英]WP_Query get post by category id returns all categorries

我正在嘗試編寫一個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)。

This is the category URL from the admin panel: 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

我嘗試用'tag_ID' => 24,交換'cat' => 24,查詢參數(來自類別頁面中的類別 URL)但結果是相同的。

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

你可以試試這個

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM