The get_categories api can be used to solve this problem. If you take a look at the reference page, you will find there is a parameter “hide_empty” which controls whether the empty category will return or not. So you may want to write something like this.
$args=array(
‘hide_empty’ => ‘0’
};
$categories=get_categories($args);
That’s it. You get all the categories.
For the collapse effect, you can use JavaScript to control the value of style.display.
@ebarney
Did you get it to work?
It didn’t look like it from
http://librarytest.kentlaw.edu/blogs/tarlock/index.php/archives/
But I wasn’t sure
I am going to dive into the plugin code now and try to get it to display empty cat/subcats, if it works, I’ll post back here.
@xs650
Actually, the categories tree on EBarney’s link was not made by the Collapsing Categories plugin. I retrieved the category items with the api get_categories and build the tree in php. All the scripts were put in the theme file we made.
Below is the scripts we use to display the categories. There are some lines of code that will replace the original rss with feedburner’s. But it should work fine even if you don’t have feedburner plugin.
<?php
$args=array(
'orderby' => 'id',
'hide_empty' => '0' // this is the option to get empty category
);
$categories=get_categories($args);
$map_parent = array();
//create a mapping array to store parents and children
//example: map_parent[23] = {24,25,26,27}
foreach($categories as $category) {
if($category->parent == 0) continue;
if(!isset($map_parent[$category->parent])){
$subcat = array();
}else{
$subcat = $map_parent[$category->parent];
}
$subcat[$category->term_id] = $category;
$map_parent[$category->parent] = $subcat;
}
//A loop for the categories who are at the highest level
foreach($categories as $category) {
if($category->parent != 0) continue;
display_entry($category, $map_parent);
$sub_cat = $map_parent[$category->term_id];
if( sizeof($sub_cat) != 0) {
echo '<div id = '.$category->term_id.'style="display:none">';
echo '<ul style="margin-left:20px;">';
hierarchy_loop($sub_cat, $map_parent);
echo '';
echo '</div>';
}
}
//This is a recursive function
function hierarchy_loop($category_list, $map_parent){
foreach($category_list as $cat){
display_entry($cat,$map_parent);
$sub_cat = $map_parent[$cat->term_id];
if(sizeof($sub_cat) != 0) {
echo '<div id = '.$cat->term_id.' style="display:none">';
echo '<ul style="margin-left:20px;">';
hierarchy_loop($sub_cat, $map_parent);
echo '';
echo '</div>';
}
}
}
//This function is used to display all the content in each category
function display_entry($category,$map_parent){
global $feedburner_settings;
echo '<li>';
echo '<a>term_id ) . '" title="View all posts in '. $category->name . '">' . $category->name.'</a>';
//Get feedburner url. If exists, use feedburner. Otherwise use the original one.
$rss_uri = htmlentities($feedburner_settings['feedburner_category_'.$category->term_id]);
if(trim($rss_uri) != ''){
$uri = str_replace('http://feeds.feedburner.com/','',$rss_uri);
echo '(<a>term_id]).'">RSS</a>)';
echo '(<a href="http://feedburner.google.com/fb/a/mailverify?uri='.$uri.'&loc=en_US">E-mail</a>)';
}else{
echo '(<a>term_id ).'">RSS</a>)';
}
echo '('. $category->count .')';
if(array_key_exists($category->term_id,$map_parent)){
echo '<a>term_id.'" href="javascript:void();"onclick="javascript:toggle('.$category->term_id.')"><font color="black"> ◄</font></a>';
}
echo'</li>';
}
?>
<script language="javascript">
var $myjQuery = jQuery.noConflict();
function toggle(id){
var subcat=document.getElementById(id);
var collapse = document.getElementById('a'+id);
if(!subcat||!collapse)return true;
if(subcat.style.display=="none"){
$myjQuery('#'+id).slideToggle('slow');
collapse.innerHTML = '<font color="black">▼</font>';
} else {
$myjQuery('#'+id).slideToggle('slow');
collapse.innerHTML = '<font color="black">◄</font>';
}
return true;
}
</script>
@jamc
Trying to get your code to work
Something in the line
echo '<a>term_id ) . '" title="View all posts in '. $category->name . '">' . $category->name.'</a>';
is fouling up the php
Can you have a look?
I’m not getting it
@xs650
I am sorry that I wasn’t check carefully after I paste code here. It seems something miss on that line. The correct one should be this:
echo '<a href="' . get_category_link( $category->term_id ) . '" title="View all posts in '. $category->name . '">' . $category->name.'</a>';