WordPress.org

Ready to get started?Download WordPress

Forums

How to display the child category of a specific parent category... (18 posts)

  1. Joshuwar
    Member
    Posted 5 years ago #

    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!

  2. Joshuwar
    Member
    Posted 5 years ago #

    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.

  3. Edward Caissie
    Theme Review Admin
    Posted 5 years ago #

    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.

  4. Joshuwar
    Member
    Posted 5 years ago #

    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!

  5. Joshuwar
    Member
    Posted 5 years ago #

    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.

  6. Edward Caissie
    Theme Review Admin
    Posted 5 years ago #

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

  7. Joshuwar
    Member
    Posted 5 years ago #

    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

  8. Joshuwar
    Member
    Posted 5 years ago #

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

  9. shakingpaper
    Member
    Posted 4 years ago #

    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!

  10. mandawahoo
    Member
    Posted 4 years ago #

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

  11. Pete
    Member
    Posted 4 years ago #

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

  12. Joshuwar
    Member
    Posted 4 years ago #

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

  13. KerriM
    Member
    Posted 4 years ago #

    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.

  14. nnlevy1
    Member
    Posted 4 years ago #

    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>'; }} ?>

  15. Pedro
    Member
    Posted 4 years ago #

    @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!!

  16. Pedro
    Member
    Posted 4 years ago #

    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?

  17. kristinachilds
    Member
    Posted 4 years ago #

    if you are trying to do this on a single.php there is a much easier way. in the loop, simply put

    <?php the_category(' / ',',single,') ?>

    where "/" is your deliniator and "single" tells wordpress to only collect the children. the other option for this is "multiple" (which pulls both parent & child). so if you wanted to display items in the parent category "ingredients" by listing them with a comma, you would insert

    <?php the_category(' , ',',single,') ?>

    or if you wanted to list all the post tags including parent separated by a ">" you would write

    <?php the_category(' > ',',multiple,') ?>

    this only works for single categories if there is only ONE parent/child category chosen for you post. if you had tagged your post with the parent category "chicken" and "recipes," both parents would show. but using the 'single' callout, it would show only the children of "chicken" and "recipes"

  18. Pedro
    Member
    Posted 4 years ago #

    Using that code:

    <?php foreach((get_the_category('orderby=ID&order=ASC')) as $childcat) {if (cat_is_ancestor_of(8, $childcat)) {echo 'cat_ID).'">';
    echo $childcat->cat_name . '
    ยป ';}}?></span>

    How can I make to short it by ID ??
    I am trying with
    ..get_the_category('orderby=ID&order=DESC')...
    but it doesnt work!

    Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic