How to show only subcategories posts in category.php?
-
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.
Code:
<?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”>
<?php
//# 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
$catid=$category[0]->category_parent;
if($catid==0){
$catid=$category[0]->cat_ID;
}</p>
<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):
echo
‘
<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.’
</div></div>
‘</p>
<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>
<p> endif;
}</p>
<p>?></p>
<p> </div>
</div>
<?php get_footer(); ?>
`
- The topic ‘How to show only subcategories posts in category.php?’ is closed to new replies.