WordPress: Spit Out List Of Child Categories?

This code spits out a list of categories, however I only need to show child categories of the current category being viewed. Any ideas?

    <ul class="categoryNav">
        <?php 
            $args = array(
            'show_option_all'    => '',
            'orderby'            => 'name',
            'order'              => 'ASC',
            'style'              => 'list',
            'show_count'         => 0,
            'hide_empty'         => 1,
            'child_of'           => 0,
            'feed'               => '',
            'feed_type'          => '',
            'exclude'            => '',
            'exclude_tree'       => '',
            'include'            => '',
            'hierarchical'       => 1,
            'title_li'           => __( '' ),
            'show_option_none'   => __( '' ),
            'number'             => null,
            'echo'               => 1,
            'depth'              => 1,
            'current_category'   => 0,
            'pad_counts'         => 0,
            'taxonomy'           => 'product_cat',
            'walker'             => null
            );
            wp_list_categories( $args ); 
        ?>
    </ul>

Related posts

2 comments

  1. I found the following seems to work:

    $cat = get_queried_object();
    $cat_id = $cat->term_id;
    $args = array(
     'style' => 'list',
        'hide_empty' => 1,
        'child_of' => $cat_id,
        'hierarchical' => 1,
        'depth' => 1,
        'taxonomy' => 'product_cat' 
    );
    wp_list_categories( $args ); 
    

    I hope others find this helpful :] This method doesn’t seem to be documented anywhere else as far as I’m aware. It’s perfect for category navigation.

  2. You have the child_of query parameter set to 0. Set it to the category currently viewed. Like:

    $cat_id = get_query_var('cat');
    $args = array(
        ...
        'child_of'           => $cat_id,
        ...
    );
    wp_list_categories( $args ); 
    

Comments are closed.