• I’m working on setting up a site that uses a very large set of hierarchical categories. It has 12 parent categories beyond the default Uncategorized, but many many subcategories, up to four levels deep.

    The site is intended to correspond to a textbook and the categories are set to match the table of contents, so the complex hierarchy is key to the purpose of the site.

    Because I’m just setting up this site the categories are empty at the moment, for the most part, except for a handful of sample posts assigned randomly to different categories.

    I’d like to display the empty categories because I’d like to show how the site is structured and the RSS links can be used for people interested only in subtopics of the book, who would like to receive future updates.

    Here’s a link to how it appears now (I may edit it in the future and remove the plugin/widget if I can’t get it to do what I want):
    http://librarytest.kentlaw.edu/blogs/tarlock/index.php/archives/

Viewing 5 replies - 1 through 5 (of 5 total)
  • 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.

    Jamc

    (@jamc)

    @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>
    xs650

    (@xs650)

    @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>';

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Plugin: Collapsing Categories] Can I kludge it to display empty categories?’ is closed to new replies.