显示自定义分类术语内的帖子数

Display Count of posts inside of a custom taxonomy term

所以,我附上了这个页面,目前有一个列表输出每个术语,其中 post 包含在特定术语中。

为此,我有一个名为 'episodes'

的自定义 post 类型

我有一个名为 'episode_categories'

的自定义分类法

里面还有各种术语,比如VR,Mixed Martial Arts等等。

我想要做的是显示每个术语的计数,其中包含 X 数量的 posts。

我目前将此作为我的代码。

<div class="placeholder-main relative py-64 md:py-56 min-h-screen flex">
    <div class="main my-auto w-full flex:none">
        <div class="section">
            <div class="container">

                <div class="categories flex flex-wrap xs:block">
                    <?php
                    $args = array(
                        'type'                     => 'episodes',
                        'post_status'              => 'publish',
                        'child_of'                 => 0,
                        'parent'                   => '',
                        'orderby'                  => 'name',
                        'order'                    => 'ASC',
                        'hide_empty'               => 1,
                        'hierarchical'             => 1,
                        'taxonomy'                 => 'episode_categories',
                        'pad_counts'               => false
                    );

                    $categories = get_categories($args);
                    ?>
                    <?php foreach ($categories as $category) : ?>
                        <div class="categories__item w-1/2 xs:w-full pr-5 xs:p-0 mt-10 xs:mt-6">
                            <div class="article text-2xl sm:text-xl xxs:text-base">
                                <h2 class="text-4xl sm:text-28px xxs:text-xl font-bold leading-none"><a href="<?php echo get_term_link($category); ?>" class="transition ease-out duration-300 hover:opacity-50"><?php echo $category->name; ?></a></h2>
                                <p>There are
                                    <strong>
                                        0
                                    </strong> podcasts in this category
                                </p>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
    </div>
</div>

默认情况下,函数 get_categories returns 每个类别关联的帖子数。您可以通过 category_count 访问此值。这是代码:

<p>
    <strong>
        <?= $category->category_count; ?>
    </strong>
</p>

已测试并有效