Support » Plugins » Divide categories into main and sub list

  • Hi,

    I am quite new to WorPress and I was puzzled when trying to split my categories into a main menu (main categories), shown at the top of my page, and a sub menu (sub categories) shown at the right.


    Getting the main categories out of wordPress wasn’t that hard. I simply used, <?php wp_list_cats(‘children=0&hide_empty=0’); ?>

    B. Right: CAT B1
    CAT B2
    CAT B21
    CAT B3

    However, retrieving the category sublist turned out to be a little more daunting though amd I stumbled across a number of solutions in this forum which use <?php wp_list_cats(‘child_of=’.$cat);?>. Now what this does is that it takes the ID of the active category, e.g. CAT B, and retrieves all of its sub categories, such as CAT B1, B2 etc.

    However, the problem is that as soon as you navigate deeper into the sub categories all of the cats lying above will disappear. For example if you selected CAT B2, wp_list_cats would only display CAT B21, i.e. the child of CAT B2.

    Since this didn’t come up to my requirements I created a function which always finds the top level/main category ID instead. Thus, whatever category you select in the sub category tree it will still display the entire sub list.

      <?php function category_get_top_parent_id ($child = 0) {
      global $wpdb;

      if(is_numeric($child) && $child > 0){

      $category_parent = $wpdb->get_var(“SELECT category_parent FROM $wpdb->categories WHERE cat_ID =$child”);

      if($category_parent == 0){$result = $child;}
      $result = category_get_top_parent_id($category_parent);}
      $result = 0;
      return $result;

      $topParentID = category_get_top_parent_id($cat); ?>
      <?php wp_list_cats(‘child_of=’.$topParentID.’&hide_empty=0′);?>

Viewing 1 replies (of 1 total)
  • Thank you very much, this is very helpful for the WP blog we are working on, which would mirror our static site’s hierarchy of categories.

    Still facing two stumbling blocks:

    1. An enhancement for this solution would be a category tree that expands only the children of the current category Bx.

    2. How can I replicate this on single post pages? That is, idnetify the posts affiliation with a specific category Bx, and present its sister subcats, that is all the other children of BX?


Viewing 1 replies (of 1 total)
  • The topic ‘Divide categories into main and sub list’ is closed to new replies.