The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

[resolved] List children of current category (4 posts)

  1. sophiavancouver
    Posted 9 years ago #

    I'd like to modify my archive.php template so that it shows a listing of the child categories for that category, no matter what category page I happen to be on. I'd like to avoid having to do a separate archive template for each category that has a subcategory and just use a function that always returns the child of the current category.
    WP_list_categories needs to know the ID of the category page you're on and doesn't seem to take a variable.

    I'm hoping there's a function that does this, but I haven't found one yet. Failing that, is there any way to pass the current id of the category page you're on to wp_list_categories so that this will work?

    Does anyone know how to do this?

  2. sophiavancouver
    Posted 8 years ago #

    Okay, I figured it out. This will give me a list of the child categories, no matter what the parent is.

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

    Many thanks to muskote whose code I adapted and modified to use wp_list_categories. (I've also set it to avoid printing the 'title line' so that if the category has no subcategory, this code will not return the phrase 'no categories'. Since I'm using it in the main archive.php template, I needed this.

  3. bredymer
    Posted 8 years ago #

    Thank you very much for the code. I've been searching the codex and forum for several hours now for a way to find the current category when viewing the archive. But I am stomped that there isn't a more simple way to do this.

  4. ignitionmedia
    Posted 8 years ago #

    Great, thanks for the code.

    I don't suppose you know how to add the subcategory description to that code so that they are displayed alongside each subcategory title?

Topic Closed

This topic has been closed to new replies.

About this Topic