Skip to main content
WordPress made easy with the drag & drop Total WordPress Theme!Learn More

How To Link To Current Category In WordPress

Last updated on:

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”.

$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:

$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

Yoast SEO

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):

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.

Yoast SEO Breadcrumbs

Now you can add your custom breadcrumb settings. Click save and your breadcrumbs will be displayed as you set them!

Subscribe to the Newsletter

Get our latest news, tutorials, guides, tips & deals delivered to your inbox.


  1. Location voiture agadir

    Really thank you, must the code i find in the web dont work outside the Loop.
    Thanks a lot.

  2. Dan Davies

    Awesome, thanks man, was looking for this for ages.

  3. Thomas

    Thank you!

  4. Max Sokorianski

    Thank you, it helped me a lot!

  5. Stuart Farrimond

    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?

  6. Mnac Hayrapetyan


  7. Kelly

    Solved my problem! Thanks so much!

  8. Audee

    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! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Learn how your comment data is processed by viewing our privacy policy here.