Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a number of top-level categories including "News" and "Sport". They each have multiple levels of child categories as shown below.

  • News

    • Australia
      • NSW
        • Sydney
  • Sports

    • Cycling
      • Road Cycling
      • BMX
    • Triathlon

In my loop I want to display a single category - what ever is the deepest child category of "Sport".

I have found some code on StackOverflow that does returns the deepest child category, but not from a specific category. It returns the the deepest child category from all top-level categories. So if a post is in two categories (Sydney and Triathlon) the code shows Sydney as the deepest child category. I somehow need to specify that which top-level category I want the deepest child to come from. In this case that would be "Sport" and the deepest child cat would therefore be "Triathlon".

share|improve this question
add comment

2 Answers

OK - I originally misunderstood your question.

function getLowestCategory()
{
    $postCategories = get_the_category();
    for ($I = 0;$I<sizeof($postCategories);$I++)
    {
        //this is a top level category
        if ($postCategories[$I])
        {
            continue;
        }
        for ($J=0;J<sizeof($postCategories);$J++)
        {
            //if another category lists it as its parent, it cannot be the lowest category 
            if (strcmp($postCategories[$I]->name,$postCategories[$J]->category_parent)==0)
            break;
        }
        //at this point, no other cateogry says it's its parent, therefore it must be the lowest one
        return $postCategories[$I]->name;
    }
}
share|improve this answer
    
Thanks for the help @fdsa. Your solution did indeed target the correct top-level category, but it pulls out the last child category (not necessarily the deepest). In the instance of my demo categories, "Sports" is category 2. And if I use your code of get_categories('child_of=2') I might have been wrong path when I said the code from StackOverflow was close, because –  TheLoneCuber Jun 8 '12 at 6:52
    
(I always hit the "Enter" key by habit which send my reply before I've finished it). In the instance of my demo categories, "Sports" is category 2. And if I use your code of get_categories('child_of=2'), then the "Triathlon" child category is shown for all posts (even posts that aren't in that category). I think that's the difference between get_the_category and get_categories isn't it? That get_categories returns objects for each category assigned to the post. –  TheLoneCuber Jun 8 '12 at 6:59
    
@Thao does the edit work for you? –  fdsa Jun 12 '12 at 5:30
add comment

I have found some code on StackOverflow that does returns the deepest child category, but not from a specific category

So what you are missing is, to specify a category. Why dont you do it? It's simple

$catID = get_cat_ID( 'Sports' );
$categories = get_the_category( $catID );
if ( $categories ) {
    $deepChild = get_deep_child_category( $categories );
    ?>
        <a href="<?php echo get_category_link( $deepChild->term_id ); ?>" title="<?php echo sprintf( __( "View all posts in %s" ), $deepChild->name ); ?>"><?php echo $deepChild->name; ?></a>
    <?php 
}

function get_deep_child_category( $categories )
{
    $maxId = 0;
    $maxKey = 0;
    foreach ( $categories as $key => $value )
    {
        if ( $value->parent > $maxId )
        {
            $maxId = $value->term_id;
            $maxKey = $key;
        }
    }
    return $categories[$maxKey];
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.