WordPress Parent Categories and Children Categories Manipulation
-
down vote favorite
This function I am trying to have on my client’s website really seems to be tough. I’ve had two outsourced companies unable to solve it…
I am a beginner to php so I am pretty much completely helpless, unfortunately.
Basically I have 3 levels of categories and I am trying to call them out dynamically in the sidebar.
For the sake of simplicity, the 3 levels of categories are: 1st Level 2nd Level 3rd Level
When I am on the 1st Level page I am looking to print a list of 2nd Level categories only.
When I am on the 2nd Level page I am looking to print BOTH a complete 2nd Level list AND for ONLY the SELECTED 2nd Level category, to print its sub-categories (or, its 3rd categories)
Then when I am on the 3rd Level I wish to print only the 2nd Level categories again.
So basically, I am looking to have the 2nd Level Categories on all level pages, then on the 2nd Level page to also have the selected 2nd Level print its sub-categories.
We have tried manipulating the wp_list_categories(); function for two days but to no avail. So far this is the closet thing we got to the ultimate goal:
<?php
function get_depth($id = ”, $depth = ”, $i = 0)
{
global $wpdb;if($depth == ”)
{
if(is_page())
{
if($id == ”)
{
global $post;
$id = $post->ID;
}
$depth = $wpdb->get_var(“SELECT post_parent FROM $wpdb->posts WHERE ID = ‘”.$id.”‘”);
return get_depth($id, $depth, $i);
}
elseif(is_category())
{if($id == ”)
{
global $cat;
$id = $cat;
}
$depth = $wpdb->get_var(“SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = ‘”.$id.”‘”);
return get_depth($id, $depth, $i);
}
elseif(is_single())
{
if($id == ”)
{
$category = get_the_category();
$id = $category[0]->cat_ID;
}
$depth = $wpdb->get_var(“SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = ‘”.$id.”‘”);
return get_depth($id, $depth, $i);
}
}
elseif($depth == ‘0’)
{
return $i;
}
elseif(is_single() || is_category())
{
$depth = $wpdb->get_var(“SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = ‘”.$depth.”‘”);
$i++;
return get_depth($id, $depth, $i);
}
elseif(is_page())
{
$depth = $wpdb->get_var(“SELECT post_parent FROM $wpdb->posts WHERE ID = ‘”.$depth.”‘”);
$i++;
return get_depth($id, $depth, $i);
}
}
$getthecurrentdepth=get_depth();$settings = “hide_empty=0”;
if (is_category()) {
$this_category = get_category($cat);
}echo “current depth is “.$getthecurrentdepth.””;
echo “category parent is “.$this_category->category_parent.””;
echo “current category is “.$this_category->cat_ID.””;$currentparent = $this_category->category_parent;
if (($currentparent==0)&&($getthecurrentdepth==0)){
wp_list_categories(‘orderby=id&show_count=0&depth=1&title_li=&hide_empty=0&use_desc_for_title=1&child_of=’.$this_category->cat_ID);
} else if (($currentparent>0)&&($getthecurrentdepth==1)) {
wp_list_categories(‘orderby=id&show_count=0&title_li=&hide_empty=0&depth=2&use_desc_for_title=1&child_of=’.$this_category->category_parent);
} else {
$parentcat = $this_category->category_parent;
$category_link = get_category_link( $this_category->category_parent );
echo “- “. get_cat_name($parentcat).”
“;
wp_list_categories(‘orderby=id&show_count=0&title_li=&hide_empty=0&use_desc_for_title=1&child_of=’.$this_category->category_parent);
}?>
Perhaps wp_list_categories can work? But I was told it’s too simple to make something like this work.
Any help would be greatly appreciated!
Many Thanks,
Kayla
- The topic ‘WordPress Parent Categories and Children Categories Manipulation’ is closed to new replies.