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.com/category/categoryslug/, displaying the daughter category slug but I also want the parent category included as well. Ideally as website.com/category/parentcategory/daughtercategory

How can I achieve this? Many thanks :D


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().

| improve this answer | |
  • 1
    Perfect, that is a great solution. Thanks so much for your help :) – Obsidian Mar 26 at 16:42

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.