【问题标题】:Display Count of posts inside of a custom taxonomy term显示自定义分类术语内的帖子计数
【发布时间】:2020-06-22 12:10:25
【问题描述】:

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

为此,我有一个名为“episodes”的自定义帖子类型

我有一个名为“episode_categories”的自定义分类法

在这里面我有各种各样的术语,比如 VR、综合格斗等等。

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

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

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

【问题讨论】:

    标签: wordpress foreach taxonomy-terms


    【解决方案1】:

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

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

    经过测试和工作

    【讨论】:

    • 感谢马蒂亚斯的回复。非常感谢你,但它似乎仍然不正确。例如,我的业务类别附加了 2 集,而您的代码仍然算作 1 集。
    • 可能需要重置第二个查询的postdata。我更新了我的帖子,告诉我它是否有效
    • 嗨,Mathias,你能写一个例子吗,不是 100% 我把代码放在正确的地方,但即使在重置时我仍然得到相同的结果。
    • 嗨,格伦,我找到了一个更好的解决方案来实现它。我已经测试过这个,它对我很有效,告诉我它是否也适合你!
    • 我的天哪,太疯狂了,我在 Stack Overflow、论坛上到处寻找,他们正在尝试各种使用 WP Query 的解决方案,冗长的代码,看起来你可以在 30 个字符内完成!这也对我有用,马蒂亚斯。非常感谢您尽快回复我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 1970-01-01
    • 2018-01-08
    相关资源
    最近更新 更多