admin管理员组

文章数量:1122846

I have a hierarchical categories tree, meaning up to 3-4 subcategories with posts. When clicking on a category link in the home page, I am redirected to the category page.

I need to check if there are no subcategories for the current category, then show posts, BUT if there are subcategories, then show only subcategories titles and descriptions with no posts at all. Next, if I click on a subcategory title, check again if there are child categories to that subcategory. If there are, show the titles and descriptions; if none, show the posts related to that subcategory.

What I did so far (code added to category page):

Get the ID of the current category:

   $CategoryPar = get_category( get_query_var( 'cat' ) ); 
   $cat_id = $CategoryPar->cat_ID;

Check if the current category has children/subcategories and print them:

$args = array(
    'child_of'           => $cat_id,
    'title_li'           => __( ' ' ),
    'current_category'   => 0,
    'pad_counts'         => 0,
    'taxonomy'           => 'category'
);
wp_list_categories( $args );

Now I get a list of the subcategories of the current category, but I still need to prevent the posts from showing in the parent category if it has children, so I have tried wrapping the loop in a conditional statement (also in category.php):

if ( category_has_children( $cat ) == false) :
    get_template_part( 'loop' ); 
endif;

and also in functions.php I added this:

function category_has_children( $term_id ) { 
    $children = get_term_children( $term_id, "category" );
    if ( is_array( $children ) ) {
        return $children;
    } else {
        return false;
    }
}

I have a hierarchical categories tree, meaning up to 3-4 subcategories with posts. When clicking on a category link in the home page, I am redirected to the category page.

I need to check if there are no subcategories for the current category, then show posts, BUT if there are subcategories, then show only subcategories titles and descriptions with no posts at all. Next, if I click on a subcategory title, check again if there are child categories to that subcategory. If there are, show the titles and descriptions; if none, show the posts related to that subcategory.

What I did so far (code added to category page):

Get the ID of the current category:

   $CategoryPar = get_category( get_query_var( 'cat' ) ); 
   $cat_id = $CategoryPar->cat_ID;

Check if the current category has children/subcategories and print them:

$args = array(
    'child_of'           => $cat_id,
    'title_li'           => __( ' ' ),
    'current_category'   => 0,
    'pad_counts'         => 0,
    'taxonomy'           => 'category'
);
wp_list_categories( $args );

Now I get a list of the subcategories of the current category, but I still need to prevent the posts from showing in the parent category if it has children, so I have tried wrapping the loop in a conditional statement (also in category.php):

if ( category_has_children( $cat ) == false) :
    get_template_part( 'loop' ); 
endif;

and also in functions.php I added this:

function category_has_children( $term_id ) { 
    $children = get_term_children( $term_id, "category" );
    if ( is_array( $children ) ) {
        return $children;
    } else {
        return false;
    }
}
Share Improve this question edited Jul 25, 2015 at 14:59 Gabriel 2,24810 gold badges22 silver badges24 bronze badges asked Jul 25, 2015 at 8:41 tousalltousall 13 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

while I was posting the question I saw this link How to only show posts on last child category and it helped me solve the last piece of the puzzle .

now it can be done:)))

本文标签: categoriesHow to show posts only for the last subcategory