Support » Fixing WordPress » How to show only subcategories posts in category.php?

  • Resolved modeman


    Hello, im building a website with products catalog, and i need help with category.php file.

    In my site are created 3 categories (FACE,HAIR,BODY) and category FACE has 3 subcategories (Cream,Cleaners,Scrubs)

    i want category.php work like that:
    I click on category named “FACE” than i only see 3 subcategories with links and i choose one of the subcategory, when i click on subcategory then i want to see all posts from that subcategory.

    Here is category.php screenshot, right now i have a code that shows selected category all subcategories with thubnail and link to subcategory, please can anybody help me with that.



    <?php get_header(); ?>
    <div class=”content”>
    <div class=”breadcrumbs”></p>

      <?php if (function_exists(‘wordpress_breadcrumbs’)) wordpress_breadcrumbs(); ?>

    <p> </div>
    <div class=”title”><?php single_cat_title(); ?></div>
    <div class=”cat-item-main”>
    //# first of all, you need to get the category array,
    // and I did it, putting the line bellow inside the loop
    $category = get_the_category(get_the_ID());</p>
    <p>// once you have the category array, the rest of the code will work wherever you want on the page </p>
    <p>//# getting the main category of the page
    <p>//# now letz get the children categories of the main category
    $categories = get_categories(‘child_of=’.intval($catid)); </p>
    <p>foreach ($categories as $category) {
    //# check if it is a real parent category with subcategories
    if ($category->parent ==$catid):

    <div class=”cat-item”>
    <div class=”image”>’.get_cat_icon(“echo=false&cat=”.$category->cat_ID).'</div>
    <div class=”text”>
    term_id ).'”>’.$category->cat_name.’
    <p> ;
    //# here we go, getting the subcategories
    $subcategories= get_categories(‘child_of=’.intval($category->cat_ID));
    foreach ($subcategories as $subcategory) {
    echo ‘<span class=”subcategory” style=”padding-left:12px”>’;
    echo ‘‘.$subcategory->cat_name.’</span>’;
    <p> endif;
    <p> </div>
    <?php get_footer(); ?>

Viewing 9 replies - 1 through 9 (of 9 total)
  • Michael


    Forum Moderator

    your posted code is somehow corrupted – please use the the to re-post the code – see

    to get the current category ID of a category archive, use:


    do you have any posts directly in the ‘FACE’ category?
    how many levels of child categories are you planning?

    Code repost here

    Yes i have 1 post in category ‘FACE’ its also attached to child category ‘Cream’.

    I planning only 1 level of child categories. Category -> Child category

    But i want to show posts only when i choose the child category.

    Any Suggestions?



    Forum Moderator

    what is the reason for this section:

    //# here we go, getting the subcategories
            $subcategories=  get_categories('child_of='.intval($category->cat_ID));
            foreach ($subcategories as $subcategory) {
                echo '<span class="subcategory" style="padding-left:12px">';
                echo '<a href="">'.$subcategory->cat_name.'</a></span>';

    because that would be sub-sub-cats …

    Yes this code part i will delete i dont need it.

    In your code i have an error Parse error: syntax error, unexpected $end can you please help to fix it?



    Forum Moderator

    corrected (and sub-sub code removed):

    Thanks, alchymyth for your help, code is working now.

    But i have another problem, i use plugin “Custom Fields and Types”.
    I have created custom type named “Katalogas” from witch im adding posts.

    My problem is that if i create new post and attach to sub-category “CREAMS”,that post shows on all sub-categories, how to separate posts to show only on selected sub-categories?

    My category.php code



    Forum Moderator

    try to add the current category into this query:

    $query = new WP_Query( array( 'post_type' => array( 'katalogas' ) ) );


    $query = new WP_Query( array( 'post_type' => array( 'katalogas' ), 'category__in' => array( get_query_var('cat') ) ) );

    Worked! Very thanks for your help you are awesome 🙂

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How to show only subcategories posts in category.php?’ is closed to new replies.