Question

I would like to display a list of all categories, using their hierarchy. I want to use jquery to manipulate other objects when the categories are clicked, so I need to remove the links that are added by default.

wp_list_categories does this wonderfully, automatically adding hierarchy of the sub-categories and adding the nested lists. I just do not want the links that are added by default.

Is there an alternative to wp_list_categories which does not give each category a link to its respective page?

When I tried get_categories(), it did not respect the hierarchy of the categories.

Using latest WP version.

Was it helpful?

Solution

The output of teh wp_list_categories() function is passed through a filter, which you could use to modify the resulting HTML:

$output = apply_filters( 'wp_list_categories', $output, $args );

If you want to actually modify the HTML that is generated by the function, you can extend the Walker_Category class, a good explanation can be found here http://scribu.net/wordpress/extending-the-category-walker.html

OTHER TIPS

The Walker_Category class is what ultimately worked for me. It had all the of the leverage I needed to make a custom list.

However, I just wanted to post my first attempt, that worked pretty well for a simple list. It uses nested loops.

 <div class="tab-row">
    <?php $args = array(
        'taxonomy' => 'taxonomyName',
        'parent' => 0
    );

    $categories = get_categories($args);
    $catid = array();
     foreach($categories as $category)  {

         echo '<ul class="parent-tab"><div class="parent-item">' . $category->name . '</div>';
         array_push($catid, $category->term_id);


        echo '</ul>';
    } ?>
</div>  
<div class="child-row">
    <?php 
    $countStop = count($categories);
    $i = 0;
    while ($i < $countStop) {
        echo "<ul class='child-list'>";
        $args = array(
                    'taxonomy' => 'taxonomyName',
                    'parent'   => $catid[$i]
                  );

        $categories = get_categories($args);      
             foreach($categories as $category) {
               echo '<li class="child-item">' . $category->name .'</li>';
             }
         echo "</ul>";
         $i++;  
    }

    ?>
</div>

I used get_categories() which eliminated the links, and created my taxonomy hierarchy by only displaying the parents in the first loop, and in the nested loop, pulling out the children of that parent by retrieving the parent ID.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top