• Resolved eric brockman

    (@eric-brockman)


    Is there a way I can do this without using the category ID?

    A site that I’m working on requires to have child menus listed in sidebar once the user navigates to a specific post, not listed in a drop down menu from the parent menu item.

    Any help is greatly appreciated!

Viewing 9 replies - 1 through 9 (of 9 total)
  • Well if it’s the CURRENT category you should have someway to identify it. So pass that identification to get_children().

    Thread Starter eric brockman

    (@eric-brockman)

    Thanks Andrew, is there a way, that you know of, to identify it as something like ‘current’, as in menu items?

    I’m trying to come up with a solution that will only take one snippet of code, as opposed to having to write hard code every time a new category / child category is added to the site.

    cheers,

    What is your site?

    Thread Starter eric brockman

    (@eric-brockman)

    https://ericbrockmanwebsites.com/dev3/kernel-memory-detail-2/

    This is a good example. Above is a link to a post categorized by Category (2010), Child Category (Kernel Memory).

    When the user is on this page, I’d like to have something in the sidebar that lists all the other child memories of ‘2010’

    thanks again for your time!

    Thread Starter eric brockman

    (@eric-brockman)

    I found this bit of code:

    <?php $this_cat = get_query_var('cat'); // get the category of this category archive page
    wp_list_categories('child_of=' . $this_cat . '&title_li='); // list child categories
    ?>

    Found it on this thread: https://www.ads-software.com/support/topic/list-child-categories-of-current-category-page?replies=6

    The problem is that it lists all children of all parents, instead of returning a list of children from the current cat.

    Any help?

    that bit of code only works for category archive pages, not single posts.

    for a single post, you can get the current categories with get_the_category() but because there could be more than one, getting a list of the child cats is a bit more complicated:

    <?php
    if( is_single() ) :
    $current_cats = get_the_category();
      foreach( $current_cats as $this_cat ) {
      wp_list_categories('child_of=' . $this_cat->term_id . '&title_li='); // list child categories
      }
    elseif( is_category() ) :
      $this_cat = get_query_var('cat'); // get the category of this category archive page
      wp_list_categories('child_of=' . $this_cat . '&title_li='); // list child categories
    endif;
    ?>

    https://codex.www.ads-software.com/Function_Reference/get_the_category

    Thread Starter eric brockman

    (@eric-brockman)

    Thanks Alchymyth, appreciate the help.

    This is definitely closer to what I need and helping me learn a bit about php, unfortunately it’s not working quite as needed yet.

    I put the code above in the index file as it was conditional statement, and when viewing a parent archive it works perfectly displaying the children. As soon as you click on a child archive though, an extra “No Category” list item is added to the output.

    I tried adding just this to the single.php

    $current_cats = get_the_category();
      foreach( $current_cats as $this_cat ) {
      wp_list_categories('child_of=' . $this_cat->term_id . '&title_li='); // list child categories
      }

    However that returns the correct child categories and 2 “No Category” list items.

    Through a process of trial and error / elimination, I’ve concluded that 1 of those “No Category” list items is there because of a parent category I was using to denote which items should be outputtedd on the home page – I think I can get rid of that (as a category item) and instead, use a custom taxonomy to arrange that.

    Still need to find a way of getting rid of the 2nd “No Category” list item though.

    Thanks in advance for any continued support and guidance!

    you can suppress the ‘no category’ mesage by adding another parameter to wp_list_categories():

    wp_list_categories('child_of=' . $this_cat->term_id . '&title_li=&show_option_none='); // list child categories

    or you check for child cats first, before using ‘wp_list_categories()’

    Thread Starter eric brockman

    (@eric-brockman)

    Ah, brilliant, thanks so much sir!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How do I list ALL child categories of the CURRENT category’ is closed to new replies.