• 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!

    `

Viewing 15 replies - 1 through 15 (of 17 total)
  • mores

    (@mores)

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

    not working..

    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!

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

    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

    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

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

    No one knows?

    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…

    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?

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

    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

    we can get that with css.

    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)

    Thanks for code I was looking for something similar to put in our new theme for https://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]

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘List subcategories if viewing a Category, and brothers if in subcat.’ is closed to new replies.