Reputation: 524
I've got this loop which works except for the fact it doesn't output all the categories in a hierarchical fashion. Instead they are all listed alphabetically.
<?php
$categories = get_categories('hierarchical=1&order=ASC&hide_empty=0');
foreach ($categories as $cat) {
$posts = new WP_Query( array('hierarchical' => 1, 'cat' => $cat->cat_ID));
?>
<div class="project">
<h2><?php echo $cat->cat_name; ?>/h2>
</div>
<?php
}
?>
So at the moment the output looks like this (a simple flat alphabetical list):
Alfa Romeo
Animals
Bugatti
Cars
Cats
Dogs
Mice
...but I need them to output like this (an alphabetical list but in the correct parent/sibling order):
Animals
Cats
Dogs
Mice
Cars
Alfa Romeo
Bugatti
I'd like them to appear in alphabetical order, but also showing the parent/sibling relationship.
I don't need to worry about adding a class or to the siblings or anything, (I've indented the above list just for demonstration purposes) and a flat list is still fine, just as long as it's in the correct order.
Thanks in advance.
Upvotes: 2
Views: 1154
Reputation: 9476
Use wp_list_categories
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'taxonomy' => 'category'
);
wp_list_categories( $args );
UPDATE
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'taxonomy' => 'category',
'style' => 'list',
'walker' =>new My_Category_Walker
);
class My_Category_Walker extends Walker_Category {
function start_lvl(&$output, $depth=1, $args=array()) {
$output .= "\n<div class=\"product_cats\">\n";
}
function end_lvl(&$output, $depth=0, $args=array()) {
$output .= "</div>\n";
}
function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
extract($args);
$cat_name = esc_attr( $category->name );
$cat_name = apply_filters( 'list_cats', $cat_name, $category );
$termchildren = get_term_children( $category->term_id, $category->taxonomy );
if($category->count >0 ){
$aclass = ' class="cat_has_posts" ';
}
else
$aclass = ' class="cat_has_no_posts" ';
if($category->parent != 0)
$link = ' <a '.$aclass.' href="' . esc_url( get_term_link($category) ) . '" ';
else
$link = '<a '.$aclass.' href="' . esc_url( get_term_link($category) ) . '" ';
if ( $use_desc_for_title == 0 || empty($category->description) )
$link .= 'title="' . esc_attr( sprintf(__( 'View all posts filed under %s' ), $cat_name) ) . '"';
else
$link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
$link .= '>';
$link .= $cat_name . '</a>';
if ( !empty($show_count) )
$link .= ' (' . intval($category->count) . ')';
if ( 'list' == $args['style'] ) {
$output .= "\t<div";
$class = 'cat-item cat-item-' . $category->term_id;
if ( !empty($current_category) ) {
$_current_category = get_term( $current_category, $category->taxonomy );
if ( $category->term_id == $current_category )
$class .= ' current-cat';
elseif ( $category->term_id == $_current_category->parent )
$class .= ' current-cat-parent';
}
$output .= ' class="' . $class . '"';
$output .= ">$link\n";
} else {
$output .= "\t$link<br />\n";
}
}
function end_el(&$output, $item, $depth=0, $args=array()) {
$output .= "</div>\n";
}
}
wp_list_categories( $args );
Upvotes: 1