How to list categories and subcategories in WordPress

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

Related posts

Leave a Reply

2 comments

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