WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've been on this for sometime and been having a ton of issues to find a proper way to do what I'm trying to achieve.

An Example would be as follows:

I have a featured Image with a rollover and I want it to show a single category child from under the parent category that has a depth of 2 for the location.

It would look something like this:

Places -> USA -> Washington D.C

I am trying to achieve to just get the Washington D.C or whatever City would be selected in the post for the category.

I would be reusing this in the loop for different information which each one might have their own depth and need to be in a list.

At the moment I have been using this code, which does not work as intended since it will pull all the child categories under the specified parent.

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

Any help would be super helpful i've been stuck on this for the past 3 days.


   $categories = get_the_category();
 if ( $categories ) :
 $deepChild = getLowestCategory( $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>

with the function from the other post.

share|improve this question
up vote 0 down vote accepted

This takes all the categories associated with your post and checks if they are parents of anything else. If one is not, it is the lowest category and is echoed out.

    $postCats = wp_get_post_categories(); 
    foreach($postCats as $childcat) { 

        //this is a top level category

        if ($childcat->category_parent==0) {

        for ($J=0;$J<sizeof($postCats);$J++) {

            //if another category lists it as its parent, it cannot be the lowest category 
            if (strcmp($childcat->name,$postCats->category_parent)==0)

        //at this point, no other cateogry says it's its parent, therefore it must be the lowest one

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

share|improve this answer
Take for instance, there will be alot of different countries -> cities -> neighborhoods for the loop to pick which is relevant to the chosen one. – espnicholas Jun 12 '12 at 5:20
@savunit - Take a look at the answer I gave to this very similar question...if it works for you let me know and I will make it the answer here – fdsa Jun 12 '12 at 5:33
Where are you echoing these? – fdsa Jun 13 '12 at 4:15
Yes - where are the images going to appear. Also - what do you have working right now. – fdsa Jun 13 '12 at 4:31
Had some syntax errors in the 2nd for loop...I edited my answer... does it work now? – fdsa Jun 13 '12 at 4:40

Your Answer


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.