Support » Themes and Templates » Display children categories for a specific parent that this post belongs to

  • My categories are grouped into several parents. Each parent is essentially a heading in a sidebar. Under each heading, I want to be able to list the children categories of the heading (parent) the current post is assigned to. (Not ALL the children–just the ones that apply to this post.)

    I can’t seem to figure out how to divide it up this way. I can list all the categories the post belongs to in one list, but I need more granular control.

    Can anyone help? I’m pulling my hair out, here.

Viewing 10 replies - 1 through 10 (of 10 total)
  • To be clearer, for example, I have a parent category called “Places” and then I might have children called “Alberta” and “San Francisco”.

    I have another parent named “Events” which might have the children categories “pizza night” and “sleepover”.

    So I need to display the children categories that apply to the current post in their respective locations in the sidebar.

    Hi Amber,

    Looks like the way to do this is to loop through all the categories, and only show the ones that apply to this post.

    You can test whether a post is in a category using the in_category function. Check out the examples at the end.

    If that’s greek to you perhaps you could give an example of what you have now (code), and I might be able to suggest what to replace it with.

    Hope that helps,

    Jonathan

    Hi Jonathan,

    Thanks for the help 🙂 I think you may be onto something!

    It looks like the code at the very bottom of that page (the one that “Tests if any of a post’s assigned categories are descendants of target categories”) may do exactly what I need it to do. The only trouble is, I’m not sure what to do with that function 🙂

    I get that it returns a value of true or false, but I’ll admit that I don’t know enough about programming to know how to make it display those children on return of “true”. Does that make sense?

    Like, once the function determines, “aha! This post IS assigned a category that is a descendant of the Places category!”, how do I get it to display that child category and link to it?

    Hi Amber,

    The quickest way for me to help might be for you to post the piece of code you’re using at the moment, and then I can propose changes.

    Here’s an attempt at what you need though:

    $categoryList = array();
    foreach((get_the_category()) as $category) // build list of categories for this post
    {
    $categoryList[] = $category->cat_ID;
    }
    ?><ul><?php
    wp_list_categories("include=" . join(",", $categoryList); // only show categories this post is in
    ?></ul><?php

    I admit that doesn’t actually use in_category! Had another look and this seemed closer.

    Happy to actually write you the code if this still doesn’t make sense / work.

    Just noticed I missed out a closing bracket:

    $categoryList = array();
    foreach((get_the_category()) as $category) // build list of categories for this post
    {
    $categoryList[] = $category->cat_ID;
    }
    ?><ul><?php
    wp_list_categories("include=" . join(",", $categoryList)); // only show categories this post is in
    ?></ul><?php

    Yes, I am also trying to do the same thing.

    I have a post that is in a couple of subcategories and these subcategories are under different parent categories.

    Let’s take amberlaine‘s example of a post:

    • One parent category called “Places” with children categories called “Alberta” and “San Francisco”.
    • Another parent named “Events” with children called “pizza night” and “sleepover”.

    What I want is to have under the post title heading ONLY the categories under “Places” and in the sidebar ONLY lists categories from the “Events”.

    The code given by jdkahn lists all the subcategories.

    Is there any way I can display the subcategories from each parent separately?

    This would be a great help.

    Thanks in advance.

    Cool! I have found the SOLUTION! It’s actually very simple.

    Check out this link:

    http://wordpress.org/support/topic/284057?replies=8#post-1120489

    kbuntu

    (@kbuntu)

    In case somebody is interested. I’m not a programmer but it worked for me. I have modified the code a bit so it just shows the parent category on the homepage.

    <?php
    $categoryList = array();
    foreach((get_the_category()) as $category) // build list of categories for this post
    {
    $categoryList[] = $category->cat_ID;
    }
    ?><ul><?php
    if (is_home()) {
    wp_list_categories('orderby=id&title_li=&depth=1'); // only show the parent categories on home page
    }
    else{
    wp_list_categories("title_li=&include=" . join(",", $categoryList)); // only show categories this post is in
    }
    ?>
    russell905

    (@russell905)

    I am trying to remove the initial category on this website I’m rebuilding – http://www.jarvismarketing.com.au/jarvis-new

    Example http://www.jarvismarketing.com.au/jarvis-new/?cat=7

    I just want it to go straight to ‘Capabilities’ page when the drop nav button is clicked on without the ‘excerpt. Someone in anther post said maybe I can change the excerpt to content in the includes section but I am not sure where the code is or what to change exactly.

    If anyone knows what I’m on about any help would be much appreciated

    Thanks

    kbuntu

    (@kbuntu)

    After implementing the code I have posted above I’ve noticed it will just show the current “Sub Category” of the post it was posted in and not my additional subcategories under the parent. In addition it won’t show the correct sub category when you are viewing the post and instead displays all the categories.

    After searching around on the forums here is what I’ve found:

    <?php
    if (is_home()) {
    wp_list_categories('orderby=id&title_li=&depth=1');
    }
    else{
    $category = get_the_category();
    $cat_term_id = $category[0]->term_id;
    $cat_category_parent = $category[0]->category_parent;
    $listcat = wp_list_categories('echo=0&child_of='.$cat_category_parent.'&title_li=');
    $listcat = str_replace("cat-item-".$cat_term_id, "cat-item-".$cat_term_id." current-cat", $listcat);
    if ( in_category( $cat_term_id ) || post_is_in_descendant_category( $cat_term_id )) {
    echo $listcat;
    }
    }
    ?>
    </ul>

    What does the code do: It lists all the parent categories on the Homepage. If you have created several subcategories it will display them in their belonging parent category and hide all other parent and their sub categories. The post will show the correct subcategory (and other sub categories belonging to the same parent category) but will exclude all the other sub and parent.

    Hope the explanation makes sense. Paste the code in the sidebar and try it.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Display children categories for a specific parent that this post belongs to’ is closed to new replies.