Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Hii i want that when i click on category link that show sub category if available instead of posts list If No Sub category available then show normal posts how to do this that if available sub categories only show just sub categories not any other posts Mean which extra code i add in my archive page that show sub categories if available otherwise show posts Thanks

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

use a conditional statement, for example:

$sub_cats = get_categories('parent='.get_query_var('cat'));
if( $sub_cats ) : 
//show list of child categories, for instance with wp_list_categories()
 echo '<ul>;
 wp_list_categories('title_li=&child_of='.get_query_var('cat'));
 echo '</ul>';
//or possibly using $sub_cats and a foreach loop//
 echo '<ul>';
 foreach( $sub_cats as $sub_cat ) {
   echo '<li><a href="'.get_category_link($sub_cat->term_id).'">'.$sub_cat->name.'</a></li>'; 
  }
 echo '</ul>';
else:
//the 'normal' LOOP of category.php//
endif;

http://codex.wordpress.org/Function_Reference/get_categories http://codex.wordpress.org/Template_Tags/wp_list_categories

edit: list code for sub categories added

share|improve this answer
    
Well after applying method that is not showing sub categories that is showing array how to get sub categories in list instaed of showing array ? –  realcoder Nov 4 '11 at 19:58
    
you could use wp_list_categories() with the 'child_of' parameter; or use a foreach loop with the result of $sub_cats; I edited my answer to include both possibilities. –  Michael Nov 4 '11 at 21:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.