How To Link To Current Category In WordPress

I was working on a new Premium WordPress theme today and one of the things I was including was custom breadcrumb navigation. In order for it to look best you need to have a structure like this: home > category > post title. Below is the code I used in order to get the current category of any post and link to it.
Display Category Link With Custom Code
Simply paste this code wherever you want your category link to appear. This will display a link to the first category of your post. This code can be placed in any theme template file, even outside the loop, but it won’t work when placed in functions.php unless it’s “hooked” into an action hook that runs once WordPress has initialized such as “init”.
<?php
$get_cat = get_the_category();
$first_cat = $get_cat[0];
$category_name = $first_cat->cat_name;
$category_link = get_category_link( $first_cat->cat_ID ); ?>
<a href="<?php echo esc_url( $category_link ); ?>" title="<?php echo esc_attr( $category_name ); ?>"><?php echo esc_html( $category_name ); ?></a>
Category link for Custom Taxonomy
If you want to display the first category link for custom taxonmy then the code is a bit different. For example if you are using a premium theme such as our “Total WordPress Theme” then you will notice there are custom post types like Portfolio, Staff and testimonials and some of these have custom taxonomies like “Portfolio Category”. So if you wanted to display the first category a portfolio post is in then you would do something like this:
<?php
$get_cat = wp_get_post_terms( get_the_ID(), 'portfolio_category' );
$first_cat = $get_cat[0];
$category_name = $first_cat->cat_name;
$category_link = get_category_link( $first_cat->cat_ID ); ?>
<a href="<?php echo esc_url( $category_link ); ?>" title="<?php echo esc_attr( $category_name ); ?>"><?php echo esc_html( $category_name ); ?></a>
Notice how in this example we used wp_get_post_terms() instead of get_the_category() ? That is because get_the_category() will only work for the core category taxonomy in WordPress not for any custom taxonomies.
How To Display Category Link With Yoast SEO Breadcrumbs
Your other option is to simply use the breadcrumbs features built into the Yoast SEO plugin. Generally when displaying the current category for a post it’s a good idea to display it in your breadcrumbs because it provides an easy navigation across your site for users, but also it can assist in your SEO efforts. Many free and premium WordPress themes actually use and recommend Yoast SEO for adding breadcrumbs because it is easy & effective.
To use the Yoast SEO breadcrumbs feature you’ll first need to make sure your WordPress theme is compatible. If it’s not this is easy to fix. Simply paste the following code into your theme file where you want to show your breadcrumbs (usually single.php or page.php above the page title):
<?php
if ( function_exists('yoast_breadcrumb') ) {
yoast_breadcrumb('<p id="breadcrumbs">','</p>');
}
?>
Once your theme is ready you can log into WordPress and go to SEO > Advanced > Breadcrumbs.
Now you can add your custom breadcrumb settings. Click save and your breadcrumbs will be displayed as you set them!
Really thank you, must the code i find in the web dont work outside the Loop.
Thanks a lot.
Awesome, thanks man, was looking for this for ages.
Thank you!
Thank you, it helped me a lot!
Brilliant! Thanks, Wondered how do I now get a link for the root category (top-level parent) rather than the immediate parent category? Any ideas?
Hi Stuart,
Have a look at the get_category_parents function.
Thanks
Solved my problem! Thanks so much!
This helps me alot since my client’s web server couldn’t support breadcrumb functionality. So I had to be creative to link a single post to its category archive page. which also not actually the category page, as I had to populate the posts to other kind of archive page :D. The category link then redirected to that other kind of archive page.
Thank you! 🙂