WordPress.org

Ready to get started?Download WordPress

Forums

Show subcategories only for selected category (21 posts)

  1. levani01
    Member
    Posted 5 years ago #

    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

  2. nanny23
    Member
    Posted 5 years ago #

    i am suffering from the same problem, would appreciate some advice!

  3. levani01
    Member
    Posted 5 years ago #

    Please help somebody,

    The example of what I want to do in on Yahoo Answers... When you select one category there are only it's subcategories in sidebar. How can I do the same with wordpress?

  4. jkirker
    Member
    Posted 5 years ago #

    I need the same - is there a plugin for this? Notice this is marked as resolved but I don't see a resolution...

  5. kenstepp
    Member
    Posted 5 years ago #

    Can I add Adsense to my profile ? Sorry I'm new here.
    Ken Stepp

  6. minihipster
    Member
    Posted 5 years ago #

    Similar (now closed) question here:
    http://wordpress.org/support/topic/136376?replies=1

    Any ideas?

  7. RichyBlue
    Member
    Posted 5 years ago #

    I am also interesed in this. This must be a future in wordpress ! They lack at the categry part in rest they rock

  8. karl19
    Member
    Posted 5 years ago #

    This might be what you're after:

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

    This is the reference page, where they have a simpler example. This doesn't list sub-categories when on a particular sub-category page though. However, in the comments, there is a snippet which is similar to the above.

    This works for me, although I'd like to re-write it so that it doesn't show on the front page, nor prints out "No categories" when there are no sub-categories.. but it's a start and might help someone out there.

  9. bustherh
    Member
    Posted 5 years ago #

    <?php wp_list_categories('orderby=id&show_count=1
    &use_desc_for_title=0&child_of='.$cat); ?>
  10. Njualem
    Member
    Posted 5 years ago #

    The example of what I want to do in on Yahoo Answers... When you select one category there are only it's subcategories in sidebar. How can I do the same with wordpress?

    I am also eager to know a solution to this problem.

    Someone help please.

  11. hquadrat
    Member
    Posted 5 years ago #

    I can't believe my eyes! I've been searching on google for an hour or two and this questions here does not seem to be answered - even collapsing categories should be a basic functionality.

    After such a long search I guess WordPress can not produce this behaviour with the base install and so has to be extended - has anyone found a better solution than

    http://wordpress.org/extend/plugins/folding-category-widget/
    ?

    Rgds, Henning

  12. hquadrat
    Member
    Posted 5 years ago #

    *er*, and this

    http://www.ciprian-dobrea.com/2009/01/fast-cat-is-fast/

    but for the moment I can't get one or the other do what I want (they should by description but both fail due to misc errors). We'll see.

  13. carl-johan
    Member
    Posted 5 years ago #

    Karl19 -

    <?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) { ?>

      <?php echo $this_category; ?>

    <?php } ?>

    Thank you so much! This works fine for me, I've been looking all over for something like this.

    Does anyone have an idea on how to tweak that code to display the Category Descriptions instead of the Name?

  14. gkMotion
    Member
    Posted 5 years ago #

    Haven't fully explored this but the documentation for wp_list_categories is at http://codex.wordpress.org/Template_Tags/wp_list_categories#Display_or_Hide_the_List_Heading

    About 3/4 down the page their is the description of the Parameter:

    use_desc_for_title
    (boolean) Sets whether a category's description is inserted into the title attribute of the links created (i.e. Category Description" href="...). The default is true (category descriptions will be inserted). Valid values:

    * 1 (true) - default
    * 0 (false)

    Is this what you're looking for?

  15. carl-johan
    Member
    Posted 5 years ago #

    gkMotion:

    Thanks for taking an interest in this!

    The "use_desc_for_title" is not really what I'm looking for. What it does is it outputs the Category Description as the title of the link, i.e. the text that shows up in a little box when you hover the link.
    What I'm looking for is a way to output the description instead of the name of the Category as the actual link to that specific category.

    MichaelH here on the forum has helped me out a bit with this.
    His code does the trick I'm looking for, but only while in a Top Category, it returns nothing while in a sub-category.

    <?php
    $cat = get_query_var('cat');
    $categories=get_categories('child_of='.$cat);
    if ($categories) {
     foreach($categories as $term) {
       echo '<a href="' . get_category_link( $term->term_id ) . '" title="' . sprintf( __( "View %s" ), $term->description ) . '" ' . '>' . $term->description.'</a>';
     }
    }
    ?>

    Any ideas are highly appreciated!
    Thanks again!

  16. design-is
    Member
    Posted 5 years ago #

    I guess no one has a solution for this yet?

    I'll keep my eyes peeled :)

    /Doug

  17. Andrey Sapegin
    Member
    Posted 5 years ago #

    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>
  18. omnijh
    Member
    Posted 5 years ago #

    @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

  19. blogsaranr
    Member
    Posted 5 years ago #

    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, http://saranr.com
    or contact me on Saranr(at)saranr.com

  20. maxoud
    Member
    Posted 4 years ago #

    get_category_children() is deprecated.

  21. fiveforty
    Member
    Posted 4 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic