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
1 Answer
Reset to default 0while 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
版权声明:本文标题:categories - How to show posts only for the last subcategory? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736287729a1927943.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论