How to show subcategories in categories else show posts

I have hierarchy like this:
Category1
-Subcategory1
-Subcategory2
–Post1
Category2
-Subcategory1

I need to show subcategories name and description when i go to category//// Now i use this code:

    <?php if (is_category()) {
    $this_category = get_category($cat);
    if (get_category_children($this_category->cat_ID) != "") {
        echo '<div id="catlist"><ul>';
            $childcategories = get_categories(array(
                'orderyby' => 'name',
                'hide_empty' => false,
                'child_of' => $this_category->cat_ID
            ));

        foreach($childcategories as $category) {
            echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>';
            echo '<p>'.$category->description.'</p>';
        }
        echo '</ul></div>';
    }
}
?>

Now i have subcategories list when click on category and blank page if i click on subcategory link but i need to show posts when i click on subcategory list.

4 s
4

Your code checks for child categories and shows them if they exist, but you aren’t then going on to process the loop that would ordinarily display the posts in the current category.

You want to extend your code to be of the form:

if ( is_category() ) {

    $this_category = get_category($cat);

    if ( get_category_children( $this_category->cat_ID ) != "" ) {

        // display the list of child categories as you currently do

    } else {

        /* run the standard loop to show the posts using
           whatever loop code your other templates use
        */

    }
}

Leave a Comment