WordPress.如何在 taxonomy.php 页面中获取自定义 post_type 计数 [英] Wordpress. How to get the custom post_type count in taxonomy.php page

查看:27
本文介绍了WordPress.如何在 taxonomy.php 页面中获取自定义 post_type 计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 WordPress v5.7 中,我有自定义分类法和自定义 post_type(歌曲、诗歌).在 taxonomy.php 模板中,我想要每个 post_type 帖子的数量.

In my WordPress v5.7, I have custom taxonomies and custom post_type's (song, poem). In taxonomy.php template I want the count of each post_type posts.

我有下面的代码,它给了我查询对象/术语的总数:

I have the below code which gives me the total count of queried 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;

如何获取当前学期的歌曲总数和诗歌总数?

How can I get the total songs and total poems count in the current term?

推荐答案

您可以使用 WP_Querytax_query.检查下面的代码.

You can use WP_Query and tax_query. check code below.

$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.如何在 taxonomy.php 页面中获取自定义 post_type 计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆