• Is it possible to show subcategoris only for the category which is being viewed. I have a lot of categories and all of them have their subcategories, but I don’t want to show them on main page. I want to show subcategories only for that category which is viewed by visitor. Is it possible

Viewing 5 replies - 16 through 20 (of 20 total)
  • Take my solution:

    Put this into …/themes/your_theme/functions.php:

    function get_mymenu($curcat = 0, $depth = 0)
    {
    	$breadcats = my_get_breadcats($curcat);
    	$my_cats = my_get_all_categories();
    	$html = "<ul class=\"children\">";
    	$categories = $my_cats[$breadcats[$depth]];
    	foreach ($categories as $cat)
    	{
    		$html.= "<li class=\"cat-item\"><a href=\"". get_category_link( $cat->cat_ID ) ."\">". $cat->cat_name ."</a>";
    		if ($cat->cat_ID == $breadcats[$depth+1] && $depth<count($breadcats))
    		{
    			$html.= get_mymenu($curcat, $depth+1);
    		}
    		$html.= "</li>\n";
    	}
    	$html.= "</ul>\n";
    	return $html;
    }
    
    function my_get_all_categories()
    {
    	static $my_cats = array();
    	if (count($my_cats)==0)
    	{
    		$categories = get_categories('child_of=0');
    		foreach ($categories as $cat)
    		{
    			$my_cats[$cat->category_parent][$cat->cat_ID] = $cat;
    		}
    	}
    	return $my_cats;
    }
    
    function my_get_breadcats($curcat = 0)
    {
    	static $breadcats = array();
    	if (count($breadcats)==0)
    	{
    		$curcat = intval($curcat);
    		$breadcats[] = $curcat;
    		while($curcat!=0)
    		{
    			$curcategory = get_category($curcat);
    			$curcat = intval($curcategory->category_parent);
    			$breadcats[] = $curcat;
    		}
    		$breadcats = array_reverse($breadcats);
    	}
    	return $breadcats;
    }

    And that into template:

    <div class="block">
    			<h3>My menu</h3>
    <?= get_mymenu($wp_query->query_vars['cat']);?>
    		</div>

    @karl 19 and carl-johan

    I managed it with the code you suggested Karl19.. yet i experience exactly the same problems as you do : I’d like it not to show on ‘front page’ and would like to get rid of the no categories mention.
    But it is definitly what we were after. Thanks for this one Karl !

    Now what I am trying to do is to add title_description as text next or under each subcategory.. and am such a beginner I can’t seem to find how to implement such function to the above code..

    <?php echo category_description( $category ); ?>

    I’m trying to get this line to fit inside the code Karl kindly shared here :

    <?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 } ?>

    but i can’t seem to find the proper syntax…

    Would be awesome if someone could help out figuring how to insert the descriptions bits

    thanks in advance for any further help

    its simple, why you people write big coding for this, just use i mentioned below,

    <?php 
    
    if (is_category()) {
      $this_category = get_category($cat);
      if (get_category_children($this_category->cat_ID) != "") {
        wp_list_categories_business('orderby=id&depth=1&show_count=1&title_li=
    &use_desc_for_title=1&child_of='.$this_category->cat_ID);
      }
    }
    ?>

    for more help pls visit, https://saranr.com
    or contact me on Saranr(at)saranr.com

    get_category_children() is deprecated.

    I’m trying to use the aboves codes, but I cant get any of them to work unless and get rid of all the widgets I have on my site.

    Is there anyway around having to get rid of the other widgets just to be able to use this feature??

Viewing 5 replies - 16 through 20 (of 20 total)
  • The topic ‘Show subcategories only for selected category’ is closed to new replies.