Search code examples
wordpresshierarchycategories

Wordpress categories hierarchy


I can't seem to find out why this code doesn't output the categories in hierarchy:

<ul>
<?php  
$args = array(
    'show_option_all'    => '',
    'container'           => false, 
    'orderby'            => 'name',
    'order'              => 'ASC',
    'hide_empty'         => 0,
    'use_desc_for_title' => 0,
    'child_of'           => 0,
    'hierarchical'       => 1,
    'number'             => null,
    'echo'               => 1,
    'depth'              => -1,
    'taxonomy'           => 'category'

); 
$categories = get_categories( $args );
foreach ( $categories as $category ) {
    echo '<li><a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '(' . $category->count . ')</a></li>';
}
?>
</ul>

Instead all the list items are outputting as parents like this...

<ul>
    <li><a href="http://test.dev/category/media/">Media(1)</a></li>
    <li><a href="http://test.dev/category/media/movies/">Movies(1)</a></li>
    <li><a href="http://test.dev/category/media/tv-shows/">TV Shows(1)</a></li>
    <li><a href="http://test.dev/category/uncategorised/">Uncategorised(1)</a></li>
</ul>

...but they should be like this...

<ul>
    <li><a href="http://test.dev/category/media/">Media(1)</a>
        <ul>
          <li><a href="http://test.dev/category/media/movies/">Movies(1)</a></li>
          <li><a href="http://test.dev/category/media/tv-shows/">TV Shows(1)</a></li>
        </ul>
    </li>
    <li><a href="http://wordpress.dev/category/uncategorised/">Uncategorised(1)</a></li>
</ul>

As you can see 'hierarchical' is set to 1, but it doesn't work as expected.

PS: I can't use the standard wp_list_categories method (http://codex.wordpress.org/Template_Tags/wp_list_categories) because I will need to be able to customise the markup in the list.

Any suggestions will be helpful.


Solution

  • You can use following code:

    <ul>
    <?php  
    $args = array(
        'show_option_all'    => '',
        'container'           => false,
        'orderby'            => 'name',
        'order'              => 'ASC',
        'hide_empty'         => 0,
        'use_desc_for_title' => 0,
        'child_of'           => 0,
        'hierarchical'       => 1,
        'number'             => null,
        'echo'               => 1,
        'depth'              => -1,
        'taxonomy'           => 'category'
    
    );
    $categories = get_categories( $args );  
    foreach ( $categories as $category ) {  
        if($category->parent==0)
        echo '<li><a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '(' . $category->count . ')</a></li>';
        else
        echo '<ul><a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '(' . $category->count . ')</a></li></ul>';
    }
    ?>
    </ul>
    

    UPDATE

    $args = array(
    
        'hide_empty'         => 0,
        'echo'               => 1,
        'taxonomy'           => 'category',
        'hierarchical'  =>1,
        'show_count' => 1,
    
    );
    
    function add_class_wp_list_categories($wp_list_categories) {
            $pattern = '/<li class="/is';
            $replacement = '<li class="first ';
            return preg_replace($pattern, $replacement, $wp_list_categories);
    }
    add_filter('wp_list_categories','add_class_wp_list_categories');
    
    echo wp_list_categories( $args );