Check child/parent categories if exists

i am serching a WP function that controls is there any child category/ies of current category (in category.php outside of the loop) and if there is, just add link list for that category/ies to page, otherwise (there is no child category for current) list all post(s) in this category.
Thanks in advance…

Related posts

Leave a Reply

1 comment

  1. You can get the current posts category outside the loop by using the get_the_category function.
    http://codex.wordpress.org/Function_Reference/get_the_category

    When you have the actual category ( however you got it) you can use get_categories, specifically the ‘child_of’ parameter and pass it the parent cat ID.
    http://codex.wordpress.org/Function_Reference/get_categories

    Also have a look at wp_list_catagories,
    http://codex.wordpress.org/Template_Tags/wp_list_categories#Display_or_Hide_the_List_Heading
    where you can do something simple like the following to grab the children.

     <?php wp_list_categories('orderby=id&show_count=1&use_desc_for_title=0&child_of=8'); ?>
    

    You would probably do an “if” statement to get the children if they exist and if not just show the category.