• Hi,

    I’m trying to get the child categories of a chosen parent to display- in the loop.

    For example, imagine there are two parent-categories called ‘Colour’ and ‘Shape’, with child-categories like ‘Red’ or ‘Green’ and ‘Square’ or ‘Triangle’. I want each post to display these child categories in specific places on my template, so it’s easy to see the Colour and Shape of each post, without cluttering it up with any other categories etc.

    But it’s proving a little tough, as the_category doesn’t seem to allow for exclusions etc in the same way as wp_list_categories– which doesn’t work inside the loop, obviously.

    So I’ve been digging around the template tags and I think in_category is on the right track, but I’m not at all hot with php so I can’t really see a way to make it spit out the children, so to speak.

    Any help anyone can offer would be hugely fantastic, even just a clue.

    Thanks!

Viewing 15 replies - 1 through 15 (of 17 total)
  • Thread Starter Joshuwar

    (@joshuwar)

    Hi!

    Well, I’ve been digging around and I’ve found a few things that are halfway similar to what I’m trying to do here and there on the forums.

    But these aren’t trying to do the exact same thing I am; and as such I am totally at a loss. Almost. Actually, I’m kinda halfway there now, as I’ve added the post_is_in_descendant_category function and I’m using that to spit out info.

    Now its just a case of getting the thing to output the subcategory’s name, and my syntax/tag knowledge forbids me this pleasure. Here’s my code for finding out whether a post is in the parent category:

    <?php if ( post_is_in_descendant_category(7) ) {
    echo "Bongo is in 7!";
    }
    else {
    echo "Bongo is not in 7!";
    }
    ?>

    What I really need help with is replacing this Bongo stuff with the_category_which_is_the_descendant_of_the_parent_I_have_chosen

    But that doesn’t seem to work.

    Any assistance would again massively appreciated.

    Have you looked at the function here:

    http://codex.wordpress.org/Function_Reference/get_the_category

    That may get you what you are looking for.

    Thread Starter Joshuwar

    (@joshuwar)

    Hey cais, thanks for the response!

    I have looked there, yes- but I’ve not been able to quite imagine how to use those functions. Do you think I could somehow combine it with the post_is_in_descendant_category function?

    Hmmm. Actually, looking at it again, I think brockangelo’s code here is exactly what I’m looking for- I’m just unable to make it work- just breaks, annoyingly.

    Any ideas why it might not be working for me?

    <?php
    $deptID = "7";
    $childCatID = $wpdb->get_col("SELECT term_id FROM $wpdb->term_taxonomy WHERE parent=$deptID");
    if ($childCatID){
    foreach ($childCatID as $kid)
    {
    $childCatName = $wpdb->get_row("SELECT name, term_id FROM $wpdb->terms WHERE term_id=$kid");
    echo $childCatName->term_id;
    echo $childCatName->name;
    }
    ?>

    Thanks!

    Thread Starter Joshuwar

    (@joshuwar)

    Ah HA! I’ve worked out how to do it. In case it’s of use to anybody, here’s how I got the children of specific parents to display, within the loop.

    Bechster’s code here got me thinking along a new track, using the is_ancestor_of boolean with variables which I didn’t understand how to manipulate or output. But actually its really simple:

    <?php
    foreach((get_the_category()) as $childcat) {
    if (cat_is_ancestor_of(7, $childcat)) {
    echo $childcat->cat_name;
    }}
    ?>

    It takes the categories of the post being displayed, checks to see if one is an ancestor of the chosen parent (‘7’ in my case), and outputs the name of that child category in the echo.

    An interesting code snippet … something to keep in mind.
    Well done on finding your fix!

    Thread Starter Joshuwar

    (@joshuwar)

    I’m still not EXACTLY sure what this -> means. Is it an array thingy? What does it do?

    I’m also not sure whether there might be other commands like cat_name that I’m unaware of. If anybody could point me in the right direction to educate myself, that would be wonderful.

    Cheers

    Thread Starter Joshuwar

    (@joshuwar)

    And Thanks Cais! It’s the first time I’ve ever been able to actually write a function in php- it’s satisfying to find something completely impossible and then gradually just get it. Even if you’re still not entirely sure why…

    Mike

    (@shakingpaper)

    Great work Joshuwar – exactly what I needed….almost.

    Using the above snippet, am I able to place a separator between the output categories, and can those categories be links? They are currently just text.

    Any help would be awesome!

    Excellent! This is exactly what I was looking for. Thank you for sharing!

    How would I go about making this category text display as link to that category?

    Thread Starter Joshuwar

    (@joshuwar)

    @mandawahoo: glad you found it useful!

    @shakingpaper & @perthmetro: to link to the categories just echo the category link as per bechster’s code mentioned above. For example instead of just echoing the cat_name, try this:

    echo '<a href="'.get_category_link($childcat->cat_ID).'">';
     echo $childcat->cat_name . '</a>';

    If you want to include a seperator just add it into the echo after the cat_name.

    Hope that helps…

    Joshwear, thanks so much for posting your solution! It’s exactly what I was looking for. I couldn’t figure out how to do this inside the loop on an individual post level until now.

    Question:

    I am trying to do something very similar, except it isn’t working. I want the code to display the child category of a given parent category even if the parent category is not selected. The parent category is named Material.

    <?php foreach((get_the_category()) as $childcat) { if (cat_is_ancestor_of('Material', $childcat)) { echo '<a href="'.get_category_link($childcat->cat_ID).'">'; echo $childcat->cat_name . '</a>'; }} ?>

    @joshuwar: I am trying to do the same as you, show child categories of each parent category, but it didnt work for me. In your code,

    <?php
    foreach((get_the_category()) as $childcat) {
    if (cat_is_ancestor_of(7, $childcat)) {
    echo $childcat->cat_name;
    }}
    ?>

    where do you chose the parent??
    and, what does ‘7’ means??

    thanks!!

    Ah, it worked, just problems ading the separator

    “echo ‘cat_ID).'”>’;
    echo $childcat->cat_name . ‘
    ‘;

    If you want to include a seperator just add it into the echo after the cat_name.”

    how?

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘How to display the child category of a specific parent category…’ is closed to new replies.