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 badges1 Answer
Reset to default 1There 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()
.