Glen Wheeler
Glen Wheeler

Reputation: 41

Display Count of posts inside of a custom taxonomy term

So, I have this page attached and have currently got a list outputting each Term where a post is contained in a particular term.

To do this I have a custom post type called 'episodes'

I have a custom Taxonomy called 'episode_categories'

and inside this I have various terms such as VR, Mixed Martial Arts and so on.

What I want to do is show a count of each term that has X amount of posts in it.

I currently have this as my code.

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

enter image description here

Upvotes: 1

Views: 1940

Answers (1)

Mathias Nicod
Mathias Nicod

Reputation: 130

By default, the function get_categories returns the number of posts each category are associated to. You can access this value through category_count. Here is the code :

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

Tested and works

Upvotes: 1

Related Questions