Showing Only WordPress Top Categories

My sub-categories links are not working! I decided to show only the top categories on my sidebar. The following describes how I do it:

Modify function get_categories in category.php

  • Add show_top option in $defaults.

    	$defaults = array('type' => 'post', 'child_of' => 0, 'orderby' => 'name', 'order' => 'ASC',
    

    'hide_empty' => true, 'include_last_update_time' => false, 'hierarchical' => 1, 'exclude' => '',

    'include' => '', 'number' => '', 'pad_counts' => false, 'show_top' => false);

  • Add to the sql where criteria if $show_top is true.

    	if ($show_top)
    

    $where .= ' AND category_parent = 0';

  • Modify my sidebar.php

    <?php wp_list_cats('sort_column=name&hierarchical=0&show_top=true') ?>
    

I notice the various variables used for the options (eg. $hierarchical) are never initialized anywhere. There is this odd looking statement extract($r); (see the function documentation here), now, I understand how this works! ;)

Comments

blog comments powered by Disqus