簡體   English   中英

WordPress。 如何在 taxonomy.php 中獲取多個自定義 post_type 總帖子數?

[英]WordPress. How to get multiple custom post_type total posts count in taxonomy.php?

在我的 WordPress v5.7 taxonomy.php模板中,我有以下代碼來獲取自定義post_type總帖子數。 (此處建議的代碼https://stackoverflow.com/a/66751447/3725816 )。

$term = get_queried_object();

$args = array(
    'post_type'      => 'your-post-type-name',
    'post_status'    => 'publish', // get only publish posts
    'posts_per_page' => -1, // get all posts
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'your-taxonomy-name',
            'field'    => 'term_id',
            'terms'    => $term->term_id
        )
    )
);

$AllPostByTerms = new WP_Query( $args );

echo $AllpostByTerms->post_count;

現在我的 WordPress 中有多個(許多)自定義 post_type。

有沒有一種方法可以在單個 function / 查詢中獲取每個自定義post_type的總帖子數,而不是為taxonomy.php模板中的每個 post_type 重復相同的代碼?

$args = array(
'post_type'      => ['post-type-name1', 'post-type-name2', 'post-type-name3'],
'post_status'    => 'publish', // get only publish posts
'posts_per_page' => -1, // get all posts
'tax_query' => array(
    'relation' => 'AND',
    array(
        'taxonomy' => 'your-taxonomy-name',
        'field'    => 'term_id',
        'terms'    => $term->term_id
    )
)

);

您可以簡單地在帖子類型參數中傳遞所有自定義帖子類型的數組

暫無
暫無

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

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