WordPress.org

Ready to get started?Download WordPress

Forums

List subcategories if viewing a Category, and brothers if in subcat. (18 posts)

  1. anabelle
    Member
    Posted 5 years ago #

    I've been searching for hours without finding an answer (even though i think i've seen this before).

    I have a category based site, nothing too complicated since it just uses two levels, Parent categories and children categories.All I need to do is to display a list of subctegories specific to a parent category in the sidebar when viewing it. So when Im viewing the "Parent" category i get a list of "Children" in the sidebar. That's easy right? :

    <?
    if (is_category()) {
    $this_category = get_category($cat);
    if (get_category_children($this_category->cat_ID) != "") {
    echo "";
    echo "

      ";
      wp_list_categories('hide_empty=0&title_li=&child_of='.$this_category->cat_ID);
      echo "
    ";
    }
    }
    ?> `

    But the problem i hav is that when I enter a subcategory, sinci it doesn't have children, the list disappears.

    How can I make it show a list of children categories if viewing a parent, and a list of brothers if viewing a children?

    Is there a plugin/widget that already does this?

    This is very similar to the "Show subpages even if on a subpage" thing in the codex, only with categories.

    Please help me or point me in the right direction, I know lots of people have the same question.

    Thanks!

    `

  2. mores
    Member
    Posted 5 years ago #

    try using $parent = get_category($cat->category_parent); if there are no more children

  3. mwilliams
    Member
    Posted 5 years ago #

    not working..

  4. bconradt
    Member
    Posted 5 years ago #

    Was there any resolution to this? I need to display a list of sibling categories and child categories like in the example below.

    • Sibling Category
    • Current Category
      • Child Category
      • Child Category
    • Sibling Category
    • Sibling Category

    Thanks!

  5. digital_dave
    Member
    Posted 5 years ago #

    I have also been looking for this solution...
    ... Bueller
    ... Bueller
    ...nothin?! [hmph*]

  6. itsgucci
    Member
    Posted 5 years ago #

    instead of using $parent = get_category($cat->category_parent);

    try

    $category_obj = get_category($cat);
    $parent = $category_obj -> category_parent;
    $parent_obj = get_category($parent);

    the result will be

    $cat is set to the current categories id (magically)
    $parent will be set to the parents id

    you may or not need $parent_obj

  7. carl-johan
    Member
    Posted 5 years ago #

    Did anyone solve this and made it work. What would the full code be? I'm confused regarding this:

    instead of using $parent = get_category($cat->category_parent);

    try

    $category_obj = get_category($cat);
    $parent = $category_obj -> category_parent;
    $parent_obj = get_category($parent);

    the result will be

    $cat is set to the current categories id (magically)
    $parent will be set to the parents id

    you may or not need $parent_obj

  8. Jen2swt
    Member
    Posted 5 years ago #

    I too need a fix for this. I've searched through many threads here and found nothing.

  9. Jen2swt
    Member
    Posted 5 years ago #

    No one knows?

  10. Frank.Prendergast
    Member
    Posted 5 years ago #

    I'd love to do this too. Been searching but it seems like any solution I find will only deal with one or two levels deep. There doesn't seem to be a truly dynamic solution available...

  11. To answer the original poster, I would try something similar to...

    $cur_cat = get_query_var('cat');
    	if ($cur_cat) {
    		$new_cats = wp_list_categories('echo=false&child_of=' . $cur_cat . '&depth=1&hide_empty=false&title_li=');
    		echo '<ul>' . $new_cats . '</ul>';
    	}

    ...to start with, and see if that's what you're looking for?

  12. anvesh
    Member
    Posted 5 years ago #

    Hi all,

    this is my first post here.

    plz check below code to display sub cats or brother cats in sub cat page.

    <?php
    if (is_category()) {
    $this_category = get_category($cat);
    }
    ?>
    <?php
    if($this_category->category_parent)
    $this_category = wp_list_categories('orderby=id&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->category_parent."&echo=0"); else
    $this_category = wp_list_categories('orderby=id&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0");
    if ($this_category) { ?>
    
    <ul>
    <?php echo $this_category; ?>
    
    </ul>
    
    <?php } ?>
  13. thewhatwherewhen
    Member
    Posted 5 years ago #

    Hi this is brilliant

    I was wondering if there is anyway to have these in a row rather than in a col with a | inbetween?

    Thanks!

    Claire

  14. anvesh
    Member
    Posted 5 years ago #

    we can get that with css.

  15. jwack
    Member
    Posted 4 years ago #

    both of the codes above appear to do the same thing, im wondering what the difference is or which one to use?

    Also, how would I get it to write out the parent category (so I could use it as a header)

  16. MAD000
    Member
    Posted 4 years ago #

    Thanks for code I was looking for something similar to put in our new theme for http://flashjourney.com/ that will be launched soon.

    I took your code and tweaked it a little bit. You can use the following to get the siblings of the current category:

    <br />
    <?php<br />
    if (is_category()) {<br />
    $this_category = get_category($cat);<br />
    if (get_category_children($this_category->cat_ID) != "") {<br />
    wp_list_categories('hide_empty=0&title_li=&child_of=' . $this_category->cat_ID);<br />
    }<br />
    else {<br />
    $parent = $this_category->category_parent;<br />
    echo "children of " . $parent . ": ";<br />
    wp_list_categories('hide_empty=0&title_li=&child_of=' . $parent);<br />
    }<br />
    }<br />
    ?><br />

    [signature moderated Please read the Forum Rules]

  17. r1pp3r
    Member
    Posted 4 years ago #

    Hello all,

    I followed the code shown in the last post of anvesh. I was just wondering how to solve the following:
    If i am on a parent category with no children it displays: "No categories". Can i remove that line somehow?

    I've been truying to follow MAD000's code as well, but here i'm having troubles showing the childcategories if i'm already on that specific childcategorie. if anyone knows how to resolve this, it would also help me out :)

    Can anybody point me to the right direction? Please bear in mind that i'm not quite skillfull in php ;o)

  18. Tommy White
    Member
    Posted 4 years ago #

    I am needing to change the text "No Categories" as well. The only post I was able to find stated this:

    <?php
    $cats = wp_list_categories('echo=0');
    if ( !preg_match( '/No\scategories/i', $cats ) ) { echo $cats; }
    ?>

    It seems to cause issues with the above code. If anyone could help that would be great. If there are no categories left I would like to have it same something different or be removed all together.

Topic Closed

This topic has been closed to new replies.

About this Topic