0

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

1

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

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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