WordPress.org

Ready to get started?Download WordPress

Forums

show only current child category (categories) (15 posts)

  1. general_lee
    Member
    Posted 8 years ago #

    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

  2. Ment0r
    Member
    Posted 8 years ago #

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

  3. Ment0r
    Member
    Posted 8 years ago #

    -- suggestion for devs --
    extend the "child_of" option to be defaulted to current category if is_category.
    -- end of suggestion --

    my 2 bytes.

  4. noise
    Member
    Posted 7 years ago #

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

  5. Kafkaesqui

    Posted 7 years ago #

    Yes, pretty common...

    <?php
    $cat_object = $wp_query->get_queried_object();
    $parentcat = ($cat_object->category_parent) ? $cat_object->category_parent : $cat;
    wp_list_cats("child_of=$parentcat");
    ?>

    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.

  6. noise
    Member
    Posted 7 years ago #

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

  7. Kafkaesqui

    Posted 7 years ago #

    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.)

  8. dopecoder
    Member
    Posted 7 years ago #

    thanks for the code Kafkaesqui, it worked for me perfectly. heres the link http://www.no1guide.co.nz/

  9. noise
    Member
    Posted 7 years ago #

    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!

  10. xinfo
    Member
    Posted 7 years ago #

    @Kafkaesqui

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

  11. Kafkaesqui

    Posted 7 years ago #

    "i dont want to show that"

    What *do* you want to show?

  12. noise
    Member
    Posted 7 years ago #

    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:

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

    not sure if that can be done or not.

    any help would be greatly appreciated.

    thanks

  13. warpdesign
    Member
    Posted 7 years ago #

    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?

  14. Kafkaesqui

    Posted 7 years ago #

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

  15. warpdesign
    Member
    Posted 7 years ago #

    Oh, I see it! Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic