簡體   English   中英

Wordpress。 如何在分類學中獲取自定義 post_type 計數。php 頁面

[英]Wordpress. How to get the custom post_type count in taxonomy.php page

在我的 WordPress v5.7 中,我有自定義分類法和自定義 post_type(歌曲、詩歌)。 taxonomy.php模板中,我想要每個 post_type 帖子的計數。

我有下面的代碼,它給了我查詢的 object / term 的總數:

// current term
$term = get_queried_object();
// total posts from the queried term
$get_posts = get_term($term->term_id, $term->taxonomy);
// total posts count in current term
$total_post_count = $get_posts->count;

如何獲取當前學期的歌曲總數和詩歌總數?

您可以使用WP_Querytax_query 檢查下面的代碼。

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

暫無
暫無

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

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