1

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.

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

Tried:

<?php
   $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>
 <?php 
 endif;
?>

with the function from the other post.

0

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.

<?php
    $postCats = wp_get_post_categories(); 
    foreach($postCats as $childcat) { 

        //this is a top level category

        if ($childcat->category_parent==0) {
            continue;
        }

        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)
                break;
        }

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

?>
  • 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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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