Support » Themes and Templates » show only current child category (categories)

  • My menubar is horizontal and it only shows the parent categories. This works without a problem

    I have changed in the header.php

      <?php wp_list_cats(‘children=0’); ?>

    What i want now is, if i klick on a parent category, a new
    navigation should show up, only with the children categories of the choosen parent category.

    I have tried on top of the index.php

      <?php wp_list_cats(‘child_of=$cat’); ?>

    If i type child_of=8, it works. the children categories
    of 8 are coming.

    Event testing with echo “$cat” i can see, that $cat is changing after each klick.

    But with child_of=$cat nothing is coming

Viewing 14 replies - 1 through 14 (of 14 total)
  • i did some research..

    there is no official way to get the current category ID. I looked up codex-wiki and tried all present cat-functions. none of them all offered a working option for this. if you not using permalinks, there would be a way by asking for the URL, prefixed by “is_category() {..”

    `$cat_id = addslashes(trim($PHP_GET[‘cat’]));

    but then again, it’s a nasty hack…

    in the codex-wiki they say that the function “the_category_ID()” has been replaced by “get_the_category()”, but what i get of their snippet is only false positives (mixed parent/child cats) .. no idea .. the results of that function are not even close.

    i also played around with that function on my own.
    no idea what it’s doing. this is really weird..

    why theres no way to get that cat_id??

    i actually thought out some crazy way to manage this, but not under 5-10 lines of code.. cant be ..

    — suggestion for devs —
    extend the “child_of” option to be defaulted to current category if is_category.
    — end of suggestion —

    my 2 bytes.

    I would also like to find an answer to this – actually a bit surprised its so hard – seems like a pretty common need.

    Yes, pretty common…

    $cat_object = $wp_query->get_queried_object();
    $parentcat = ($cat_object->category_parent) ? $cat_object->category_parent : $cat;

    Note that “there is no official way to get the current category ID” from above is incorrect. $cat is a global WordPress var that holds the current category ID (it’s what we set $parentcat to when the current category is not the child to another). No need to trip it out from $_GET.

    so does that mean WP should be able to do it without extra code?

    Considering I gave the extra code to do it…I think you can consider the answer to be “no.”

    (Assuming your question relates specifically to the ‘child_of’ issue, and not the category ID one that Mentor brought up.)

    thanks for the code Kafkaesqui, it worked for me perfectly. heres the link

    ah – sorry I thought it might need to go in the functions.php or something as I haven’t come across code like that yet.

    works quite well!


    your code above work fine but in home page it show no category , but i dont want to show that

    i dont want to show that

    What *do* you want to show?

    it worked well, but now when you have top category <dogs> & select to see all the dog posts, the sidebar now reads:

    <brown dogs>
    <black dogs>
    <golden dogs>

    if you then click on <black dogs> the proper post shows up but now the sub cats area reads “no categories”.

    What I’d love is a way of having the category label and navigation stay in the sidebar reading as:

    <brown dogs>
    <black dogs>
    <golden dogs>

    not sure if that can be done or not.

    any help would be greatly appreciated.




    Hi, I think this question may be related to this so I’m posting here. I want to figure out how to signify the current category when I’m using wp_list_cats. For example, I want the to be able to style the current category differently, like make it a different color. I have a feeling that I cannot do this using the ‘list’ parameter, but that I may need to create a custom loop that checks if the category is the current one, that way I could echo out something like: li class=”current”

    How can I do this?



    WordPress should already be setting the current category listed under wp_list_cats() to the class “current-cat”



    Oh, I see it! Thanks!

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘show only current child category (categories)’ is closed to new replies.