I have an affiliate site that has 4 'malls' which are Categories (ie: Baby Boutique, Everyday Baby, Discount Baby and Organic Baby), and within each of those 'malls' are a lot of product categories (ie: Strollers, Hats, etc) which are sub-categories of each of the 'malls' (parent categories).
On each page in the left hand column I have a list of Categories (sub-categories). I need to be able to show all the sub-categories listed together on the homepage without any of the parent categories, but also be able to list ONLY sub-categories from a specific parent (ie: Baby Boutique) on that categories internal pages.
I know this is a bit confusing, but to put it simply I need to be able to list all sub-categories together without showing their parent categories AND be able to show a list of sub-categories of a parent category and links to only products of that parent category.
Any Help?
Sébastien | French WordpressDesigner answers:
paste this code where you want : <?php slh_category(); ?>
For example, in a sidebar.
If it's the homepage, this function displays the list of all sub-categories (no toplevel category) ordered by name
If it's a toplevel category, this function displays the list of the sub-categories of this toplevel category, ordered by name
If it's a sub-category, this function displays the list of the sub-categories of the parent of this sub-category, ordered by name (displays the name of the current category and the names of its "sister-category")
paste this code in the file functions.php
function slh_category(){
$categories = get_categories();
if($categories) {
foreach($categories as $a_category){
if($a_category->category_parent==0) $toplevel.=$a_category->cat_ID.','; //create the array of IDs of toplevel cat
if($a_category->category_parent!=0) $notoplevel.=$a_category->cat_ID.','; //create the array of IDs of no-toplevel cat
}
}
$toplevel = substr($toplevel, 0, -1);
$notoplevel = substr($notoplevel, 0, -1);
$list_children="";
if((is_home()) OR (is_front_page())){//if it is the home we display all the category but no toplevel categories
$list_children = wp_list_categories('orderby=name&title_li=&exclude=' .$toplevel. '&include=' .$notoplevel. '&echo=0&show_option_none=' );
}
elseif(is_category()){
$thisCat = get_category(get_query_var('cat'),false);
if($thisCat->category_parent==0) { //if it's a toplevel category
$list_children = wp_list_categories('orderby=name&title_li=&child_of=' .$thisCat->cat_ID. '&echo=0&show_option_none=' );
}else{
$list_children = wp_list_categories('orderby=name&title_li=&child_of=' .$thisCat->category_parent. '&echo=0&show_option_none=' );
}
}
echo $list_children;
}
Dan | gteh answers:
This should do it for the pages when ON the category page and want to list out only sub-categories:
<?php
if (is_category()) {
$this_category = get_category($cat);
}
if($this_category->category_parent)
$this_category = wp_list_categories('orderby=id&title_li=&child_of='.$this_category->category_parent."&echo=0"); else
$this_category = wp_list_categories('orderby=id&title_li=&child_of='.$this_category->cat_ID."&echo=0");
if ($this_category) { ?>
<ul>
<?php echo $this_category; ?>
</ul>
<?php } ?>
Not sure about listing them all on the home page though
Ivaylo Draganov answers:
Hello,
Given the details you've provided I've put up a simple example on how to achieve the goal:
<?php
// set vars
$top_level_cats = '3,5,8,13'; // hardcoded list of top level category IDs
// category page
if ( is_category() ) {
// get category object
$current_cat = get_queried_object();
// sub-category
if ( $current_cat->category_parent ) {
$categories = wp_list_categories( 'orderby=id&title_li=&child_of=' . $current_cat->category_parent. '&echo=0' );
// top-level category
} else {
$categories = wp_list_categories( 'orderby=id&title_li=&child_of=' . $this_category->cat_ID . '&echo=0' );
}
// homepage
} else if ( is_home() ) {
// list categories excluding top-level ones
$categories = wp_list_categories('orderby=id&title_li=&exclude=' . $top_level_cats . '&echo=0' );
}
// print list of categories
echo '<ul>' . $categories . '</ul>';
The top level category IDs are hardcoded, but with some extra effort I believe that could be made automatic. I'm ready to work with you if you need customization or any help implementing a solution.