最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - Need to pull parent category and slug - only pulling daughter category

programmeradmin3浏览0评论

I am tweaking a theme for my friend, and I am using the following:

<?php $category = get_the_category(); ?> <h3 class="omc-blog-two-cat"><a href="<?php echo home_url(); echo ('/category/'.$category[0]->slug); ?>"><?php echo $category[0]->cat_name; ?></a></h3>

This gives website/category/categoryslug/, displaying the daughter category slug but I also want the parent category included as well. Ideally as website/category/parentcategory/daughtercategory

How can I achieve this? Many thanks :D

I am tweaking a theme for my friend, and I am using the following:

<?php $category = get_the_category(); ?> <h3 class="omc-blog-two-cat"><a href="<?php echo home_url(); echo ('/category/'.$category[0]->slug); ?>"><?php echo $category[0]->cat_name; ?></a></h3>

This gives website/category/categoryslug/, displaying the daughter category slug but I also want the parent category included as well. Ideally as website/category/parentcategory/daughtercategory

How can I achieve this? Many thanks :D

Share Improve this question asked Mar 25, 2020 at 21:16 ObsidianObsidian 376 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

There are functions for retrieving a term/category link (i.e. URL to the term archive page) and in the case of the default category taxonomy, you can use get_category_link():

<a href="<?php echo esc_url( get_category_link( $category[0] ) ); ?>"><?php echo $category[0]->name; ?></a>

For custom taxonomies, you'd use get_term_link().

发布评论

评论列表(0)

  1. 暂无评论