开发者

How to list categories and subcategories in WordPress

开发者 https://www.devze.com 2023-01-25 12:31 出处:网络
How to display all CATEGORIES and SUB-CATEGORIES in wordpre开发者_StackOverflowss if a category having 0 posts also. I tried but it is displaying categories which are having at least 1 post in a categ

How to display all CATEGORIES and SUB-CATEGORIES in wordpre开发者_StackOverflowss 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


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

<?php $cats = get_categories('hide_empty=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.

0

精彩评论

暂无评论...
验证码 换一张
取 消