Posted by on February 17, 2013

Here let me show 2 skills on how to use wordpress well.

1. show all sub-categories at its parent category page;
2. get rid of “no categories” if a category does not have sub-category.

For the first, there are 2 method, one does not show sub-category is the sub-category deos not have post:

<?php $this_cat = (get_query_var(‘cat’)) ? get_query_var(‘cat’) : 1; ?>
<?php $this_category = get_category($this_cat);
if ( $this_category->parent ) { $this_cat = $this_category->parent; } ?>
<?php wp_list_categories(‘child_of=’ . $this_cat . ”); ?>

The other is show sub-category even if the sub-category does not have post:

<?php   //show a category without post also $cat = get_query_var(‘cat’); $cat_list = wp_list_categories(‘child_of=’.$cat.’&hide_empty=0&title_li=&echo=0′);     if($cat_list) {     echo ‘Sub-category List <ul>’;     echo $cat_list;     echo ‘</ul>’;     } ?>

or:

<?php  //show a category without post also $cat = get_query_var(‘cat’); $cats = wp_list_categories(‘title_li=&echo=0&orderby=id&use_desc_for_title=1&hide_empty=0&child_of=’.$cat);

if(is_category()) : ?>

<ul> <?php echo $cats; ?> </ul>

<?php endif; ?>

For the second, get rid of “no categories”, do as follows:

In wp-includes is a file, category-template.php. On line 454 (or so) is the line: ‘show_option_all’ => ”, ‘show_option_none’ => __(‘No categories’), Replace the (‘No categories’) with (”), save the file and upload it to replace the old category-template.php file. Every time you upgrade WordPress, repeat.

or you can write your own text, instead of “no categories”.

Posted in: WordPress