Forums

Subcategories appear only in their Parent Category (4 posts)

  1. xrayzor
    Member
    Posted 2 years ago #

    Hey guys. I'm using WP as a CMS for a site and I'm using categories and subcategories for the basic structure of the site.

    IE:
    About Us (main category)
    -Directions (sub cat)
    -Contact Us (sub cat)

    Groups (main category)
    -Men's group (sub cat)
    -Women's group (sub cat)
    -Teens (sub cat)

    My question is how do I get the list of subcategories to only appear under its parent? I did this with creating custom category template + custom sidebar file but that's way too much files.

    I also used this PHP code to do the same. The only problem is that this only shows the subcats in their parents, but once I click into a subcategory, the list of links disappear.

    <?php
    
    if (is_category()) {
    	$this_category = get_category($cat);
    	if (get_category_children($this_category->cat_ID) != "") {
    		wp_list_categories('orderby=order&show_count=0&title_li=
      &use_desc_for_title=0&hide_empty=0&child_of='.$this_category->cat_ID);
    	}
    } ?>

    I want to keep using this PHP file but how do I use it so that it shows the list of subcats even though I'm in one of the subcats?

    I hope that made sense.

  2. xrayzor
    Member
    Posted 2 years ago #

    Okay I sort of got the answer.

    I have this code going:

    <?function get_parent_category() {
     global $wp_query;
     $category = $wp_query->get_queried_object();
     if ($category->category_parent) return $category->category_parent;
     else return $category->cat_ID;
    }?>
    
    <?php $id = get_parent_category();
    if ($id) {
    	echo "<ul>";
    	echo wp_list_categories("orderby=order&option_count=1&hierarchical=0&hide_empty=0&use_desc_for_title=0&child_of=".$id."&title_li=");
    	echo "</ul>";
    } else {
    	include(TEMPLATEPATH .'/sidebar-general.php');
    } ?>

    So basically I'm calling the id, and sticking the id after "child_of" and also making sure that it appears in the child categories.

    The only thing that bothers me is that it says "No Categories" whenever I go to a category without a subcat.

    How can I make it so that it doesn't show "No Categories" when no subcategories are present in that category?

    Any help here?

  3. xrayzor
    Member
    Posted 2 years ago #

    BUMP!

  4. xrayzor
    Member
    Posted 2 years ago #

    Okay so since I'm terrible at PHP, I just went into wp-includes/category-template.php

    and changed the lines (around lines 254)

    if ( 'list' == $style ) //inserted "nocats"
    			$output .= '<li class="nocats">' . __("No categories") . '</li>';
    		else

    As you can see, I inserted class="nocats" after the first list tag opener so that I could use css to display: none; to hide it.

    Just a round about hack.

Topic Closed

This topic has been closed to new replies.

About this Topic