如何在 WordPress 中列出类别和子类别

发布于 2024-10-03 02:46:23 字数 173 浏览 6 评论 0原文

如果类别也有 0 个帖子,如何在 wordpress 中显示所有类别子类别。我尝试过,但它显示的类别中至少有 1 个帖子。我想显示也有 0 个帖子的类别。

谢谢

How to display all CATEGORIES and SUB-CATEGORIES in wordpress if a category having 0 posts also. I tried but it is displaying categories which are having at least 1 post in a category . I want to display categories which are having 0 posts also.

Thank You

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮年慕年 2024-10-10 02:46:24

参见get_categories函数,有一个参数叫“hide_empty”。例如:

<?php $cats = get_categories('hide_empty=0'); ?>

See the get_categories function, there is a parameter called "hide_empty". For instance:

<?php $cats = get_categories('hide_empty=0'); ?>
世界如花海般美丽 2024-10-10 02:46:24

使用 get_categories() 函数获取所有类别。

$args = array(
    'type'                     => 'post',
    'child_of'                 => 0,
    'parent'                   => '',
    'orderby'                  => 'name',
    'order'                    => 'ASC',
    'hide_empty'               => 1,        // hide empty categories [0] false
    'hierarchical'             => 1,
    'exclude'                  => '',
    'include'                  => '',
    'number'                   => '',
    'taxonomy'                 => 'category',
    'pad_counts'               => false
);
$categories = get_categories( $args );

将所有类别存储在变量中后,使用 print_r 查看可以使用的数组的所有值。

    echo "<pre>";
        print_r( $slider_category );
    echo "</pre>";

当你使用 print_r 时,你会看到这样的

Array (
[0] => stdClass Object
    (
        [term_id] => 11
        [name] => Architecture
        [slug] => architecture
        [term_group] => 0
        [term_taxonomy_id] => 11
        [taxonomy] => category
        [description] => 
        [parent] => 0
        [count] => 3
        [cat_ID] => 11
        [category_count] => 3
        [category_description] => 
        [cat_name] => Architecture
        [category_nicename] => architecture
        [category_parent] => 0
    ))

代码,你会得到你想要的。

use get_categories() function to get all categories.

$args = array(
    'type'                     => 'post',
    'child_of'                 => 0,
    'parent'                   => '',
    'orderby'                  => 'name',
    'order'                    => 'ASC',
    'hide_empty'               => 1,        // hide empty categories [0] false
    'hierarchical'             => 1,
    'exclude'                  => '',
    'include'                  => '',
    'number'                   => '',
    'taxonomy'                 => 'category',
    'pad_counts'               => false
);
$categories = get_categories( $args );

after you have stored all categories in a variable use print_r to see all the values of the array that you can use.

    echo "<pre>";
        print_r( $slider_category );
    echo "</pre>";

when you use print_r you will see like this

Array (
[0] => stdClass Object
    (
        [term_id] => 11
        [name] => Architecture
        [slug] => architecture
        [term_group] => 0
        [term_taxonomy_id] => 11
        [taxonomy] => category
        [description] => 
        [parent] => 0
        [count] => 3
        [cat_ID] => 11
        [category_count] => 3
        [category_description] => 
        [cat_name] => Architecture
        [category_nicename] => architecture
        [category_parent] => 0
    ))

play with the code and you will get what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文