WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to display children of a category (10 posts)

  1. harveypooka
    Member
    Posted 6 years ago #

    Evening all,

    I need a way to display a list of subcategories of a category. Like this:

    "Please click a country for a list of our current projects:

    • Africa
    • Sri Lanka"

    With Projects (parent) and Africa and Sri Lanka being subcategories (children).

    When the user clicks on Projects, I'd like a list of the subcategories to appear, instead of the default list of posts in all of the categories.

    I've managed to make the page display the categories of the posts (5 Africa's and 2 Sri Lanka's due to the amount of posts), but I can't seem to find anything that'll let me display the children of a parent as a list.

    I'm guessing I can do this by using get_the_category_list() or the_category() and utilising some arcane argument/parameter, but I'm at a loss.

    Thank you.

  2. sethmorris
    Member
    Posted 6 years ago #

    You and me are in the same boat it seems, there doesn't seem to be much of anyone to explain this as I can see.
    It's pretty sad as a basic need for navigating content. Any plugins I've found just cause more problems than anything.

    Please someone, put this in a Ultra-For-Dummies-Explanation if you can.

    I'm about to look for another content solution...

  3. harveypooka
    Member
    Posted 6 years ago #

    I'm going to keep looking on the forums and brush up on some PHP tomorrow. There will be a way to do it, but it may involve some PHP hacking.

    Once I've found the solution I'll be happy to help you. I don't want to give up on WordPress as it's a bloody marvelous endeavor.

  4. harveypooka
    Member
    Posted 6 years ago #

    Well, I've taken a look around at multiple loops, a variety of plugins and even some PHP-ness, but I can't find anything.

    As this is for a charity website and I have no experience in PHP or time to learn it right now, I think I'll have to find another content solution too.

    Damn.

  5. harveypooka
    Member
    Posted 6 years ago #

    Right. I'm cheating.

    Instead of having a generated list I'm simply making it manual. I'll look into creating it from the database another time.

    I'm removing the loop and any tags and manually placing absolute links to my subcategories/children.

    Cheating, but it'll work for the time being!

  6. yakuphan
    Member
    Posted 6 years ago #

    You can use the following codes.

    $cat = 5;
    wp_list_categories("orderby=name&show_count=1
    &use_desc_for_title=0&hide_empty=0&child_of=$cat&title_li");

    And you can get a widget about Cats of Cats by this link. But language of the site is Turkish. If you want to download the widget, you can click the link in this sentence "Bileşeni buradan indirin"
    Usage the widget: Download/Upload to the plugin directory/Activate the widget and costumize your widget
    I'm sorry for my poor English.

  7. ellensmith
    Member
    Posted 6 years ago #

    I have successfully displayed children of categories on my WordPress blog by using a work-around in which I named the categories hierarchically.

    In the situation that harveypooka described, the categories would become Projects > Africa and Projects > Sri Lanka.

  8. harveypooka
    Member
    Posted 6 years ago #

    Thank you so much, Yakuphan and Ellen.

    I'll give both of these a shot tomorrow and see if they solve my problem.

    Yakuphan - where did you find your code, or information about it? The Codex seemed a little thing on information. Oh, and your English is 100 times better than my Turkish! :)

  9. yakuphan
    Member
    Posted 6 years ago #

    Hi, again.
    If you download this widget, you can show post count and see wp_list_categories function on work.

    I found my codes in this widget from this link.
    "Only Show Children of a Category" title gives the information.
    But if you want to know about "get_category_children()" function, i can't remember. Maybe this link help you. I take the child categories like "1 2 4 5", and i explode it. Of course i use a "for each".

    $childs = get_category_children($cat, $before = ' ', $after = '');
    $catts = explode (' ', trim($childs));
    foreach($catts as $catt) {
    $catname = get_cat_name($catt);
    echo '

  10. '.$catname.'
  11. ';
    }

    You can use the following codes in this widget.

    $cat = (int) $cat;
    //$cat is category id
    wp_list_categories("orderby=name&show_count=1
    &use_desc_for_title=0&hide_empty=0&child_of=$cat&title_li");

    These codes are very simple and use the WP API. Important keyword is child_of. You can costumize that code as you want.

  • r3ne94d3
    Member
    Posted 5 years ago #

    Very simple solution, no hacks or widgets.

    Add this code to wherever you want it (but customize the child_of=3 to your parent category)

    <?php wp_list_cats('sort_column=id&optioncount=0&use_desc_for_title=0&child_of=3'); ?>

    Heres an explanation of what it does:

    "<?php wp_list_cats('sort_column=id" = list categories
    "&optioncount=0" = don't include the count of articles
    "&use_desc_for_title=0" = sort by title descending
    "&child_of=3'); ?>" = all categories under the parent category 3

    Hope this help you all
    Renegade

  • Topic Closed

    This topic has been closed to new replies.

    About this Topic

    Tags