Get only the top level categories. There is no depth parameter on theĀ get_categories function, this query and foreach loop will ensure only toplevel categories are displayed:
<?php $args=array( 'taxonomy' => 'product_cat', 'hierarchical' => '1', 'hide_empty' => '0', 'pad_counts' => '1', 'orderby' => 'term_order' ); $categories=get_categories($args); foreach($categories as $category) { if($category->parent == 0 ){ echo '<p><a href="'. get_term_link($category->slug, 'product_cat') .'" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> '. $category->count . '</p> '; } } ?>
This query will get just the child categories of a given parent:
<?php $categoryID = $category->term_id; $args=array( 'taxonomy' => 'product_cat', 'child_of' => $categoryID, 'hide_empty' => '0', 'orderby' => 'term_order' ); $categories=get_categories($args); foreach($categories as $category) { echo '<p>- <a href="'. get_term_link($category->slug, 'product_cat') .'" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> '. $category->count . '</p> '; } ?>
In conjunction with the ‘Custom Taxonomy Order‘ plugin and an orderby parameter of ‘term_order’, the two queries above can be combined to return a custom ordered list of categories with multiple levels.
<?php $args=array( 'taxonomy' => 'product_cat', 'hierarchical' => '1', 'hide_empty' => '0', 'pad_counts' => '1', 'orderby' => 'term_order' ); $categories=get_categories($args); foreach($categories as $category) { if($category->parent == 0 ){ echo '<p><a href="'. get_term_link($category->slug, 'product_cat') .'" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> '. $category->count . '</p> '; $categoryID = $category->term_id; $args=array( 'taxonomy' => 'product_cat', 'child_of' => $categoryID, 'hide_empty' => '0', 'orderby' => 'term_order' ); $categories=get_categories($args); foreach($categories as $category) { echo '<p>- <a href="'. get_term_link($category->slug, 'product_cat') .'" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> '. $category->count . '</p> '; } } } ?>
Revisions
- November 5, 2012 @ 23:31:25 [Current Revision] by PeterLugg
- November 5, 2012 @ 23:26:27 by PeterLugg
No comments yet.