I'm trying to display a link to just the child category on a Wordpress post. For instance if the category for a post is parent > child I want to show a link to just the child category page.

I'm using code from here: Name of last category level for a post

It works perfectly but just prints the child category, how do I go about making it a link tot he child category?

 $allCat = get_the_category();
 $lastCat = array_reverse($allCat);
 echo $lastCat[0]->name;

1 Answer 1


You can use get_category_link() for that:

$allCat = get_the_category();
if( ! empty( $allCat ) ){
    $lastCat = array_reverse( $allCat );
    $last_cat_link = get_category_link( $lastCat[0] );
    if( ! is_wp_error( $last_cat_link ) ){
        echo '<a href="' . $last_cat_link . '">' . $lastCat[0]->name . '</a>';


  • I tried this and it shows the correct child category but the link goes to the post, not the category page. @freejack
    – Tnace
    Oct 9, 2019 at 16:48
  • I have updated the code as there was a typo in $lastCat variable. Also added extra checks to avoid errors!
    – freejack
    Oct 9, 2019 at 17:00
  • That's perfect, thanks for your help!
    – Tnace
    Oct 9, 2019 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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