Created
April 11, 2015 07:29
-
-
Save freezvd/6990d3a589b0ed5cf689 to your computer and use it in GitHub Desktop.
To achieve a pattern where Parent Category shows without count and their respective Child categories shown with count.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
This is what I wrote to achieve a pattern where Parent Category shows without count | |
and their respective Child categories shown with count. I am pretty sure that there is some neat | |
way to get this done, but I was not able to find that. Pls mention in comments if you knw any | |
other alternative approach. | |
Ex: | |
Parent | |
-Child1 (10) | |
-Child2 (99) | |
*/ | |
$noparent = array(); | |
$childs = array(); | |
$taxonomy = "category"; | |
$i=0; | |
$args = array( | |
'hide_empty' => 0, | |
); | |
$cats = get_categories($args); | |
// Gets all parent and single categories | |
foreach ((array)$cats as $cat) { | |
$catparent = $cat->category_parent; | |
if($catparent == 0) { | |
$noparent[$i]['name'] = $cat->name; | |
$noparent[$i]['id'] = $cat->term_id; | |
$i++; | |
} | |
} | |
echo "<div class='cat-content'>"; | |
// Make display of parent category (without count) and their children categories (with count) | |
foreach ((array)$noparent as $nop) { | |
$childs = get_term_children( $nop['id'], $taxonomy ); | |
if( !empty( $childs ) ) { | |
echo "<span class='catlist'>"; | |
echo "<b>" . $nop['name'] . "</b>"; | |
foreach ((array)$childs as $child) { | |
$argss = array( | |
'style' => 'list', | |
'show_count' => 1, | |
'hide_empty' => 0, | |
'child_of' => $nop['id'], | |
'title_li' => '', | |
'taxonomy' => 'category', | |
); | |
wp_list_categories( $argss ); | |
//echo "<br />"; | |
break; | |
} | |
echo "</span>"; | |
} else { | |
echo "<br />"; | |
echo "<span class='catlist'>"; | |
echo "<b>" . $nop['name'] . "</b>"; | |
echo "</span>"; | |
echo "<br />"; | |
} | |
} | |
echo "</div>"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment