categories - Link To Child Category For A Post

I'm trying to display a link to just the child category on a Wordpress post. For instance if the category for a pos

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;

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;
Share Improve this question asked Oct 9, 2019 at 16:34 TnaceTnace 173 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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>';
    }
}

https://developer.wordpress/reference/functions/get_category_link/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745095063a4610927.html

相关推荐

  • categories - Link To Child Category For A Post

    I'm trying to display a link to just the child category on a Wordpress post. For instance if the category for a pos

    14小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信