Journal of a Web Professional in Brussels

WordPress: Display current category ‘s sub-categories as a menu

If you have no interest in wordpress whatsoever, i'm sorry to announce that i'll be wordpressing a lot in the next few weeks, so you might want to consider unsubscribing from this - otherwise excellent - website's RSS feed. Sorry to see you go.

If on the contrary you dig WordPress like it's your daddy's car, you might be interested by this cute function i came up with, in need of a way to display a menu made of the present node (page, post or custom type) 's category children categories. Say that ten times aloud really fast to your neighbour, she'll appreciate your geekiness, and if you're in India, even find it sexy.

It works sidewide as long as each post is in at least one category.

PHP:
  1. function children_category_menu() {
  2.     if (is_category() && !is_front_page()) {
  3.         global $post;
  4.         $categories = get_the_category($post->ID);
  5.         if (is_array($categories) && count($categories)> 0) {
  6.             $the_cat = 0;
  7.             foreach ($categories as $c) {
  8.                 if ($c->category_parent == 0) {
  9.                     $the_cat = $c;
  10.                     break;
  11.                 }
  12.             }
  13.             /* --- Get the Children --- */
  14.             if (get_category_children($the_cat->term_id) != "") {
  15.                 $class = $the_cat->category_nicename;
  16.                 echo '<ul id="current_subcategory_menu" class="category_top_menu ' . $class . ' clearfix">';
  17.                 wp_list_categories('orderby=id&show_count=0&title_li=&use_desc_for_title=1&child_of=' . $the_cat->term_id);
  18.                 echo "</ul>";
  19.             }
  20.         }
  21.     }
  22. }

Put it in your theme's functions.php then use it wherever you want this menu to appear by calling the function

PHP:
  1. children_category_menu();

The Conversation

One Response to WordPress: Display current category ‘s sub-categories as a menu

  1. Pingback: WordPress: Display current category ‘s sub-categories as a menu | WebDino.net

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Belgians do IT better _