Technology WordPress Basics

How To Create A Link To Current Category In WordPress

Sometimes you need a dynamic link to the current category for a custom template or feature. Here is a simple way to create one in WordPress, a small snippet that comes in handy more often than you would expect.

Omkar Bhagat Omkar Bhagat 3 min read
How To Create A Link To Current Category In WordPress

While working on a client project, there was a requirement, where we had to include the name of the category and the category link in the post excerpt as well as the post itself. It’s a simple enough solution. And here’s what you need to do.

[space]

If you already know the Category Name..

To fetch the category link, you’ll have to first fetch the category ID. You can do this with the help of get_cat_ID function() function.

[pre]$category_id = get_cat_ID( ‘Category Name’ );[/pre]

Here you can get the ID of any category by specifying the name of the category, in place of ‘Category Name’. This ID is then saved inside a variable called $category_id.

[pre]$category_link = get_category_link( $category_id );[/pre]

We then pass this $category_id as a parameter in the function called get_category_link() and store the result in $category_link. Here we are basically storing the link inside a variable so as to display it later using the HTML code.

[pre]<?php
// Get the ID of a given category
$category_id = get_cat_ID( ‘Category Name’ );

// Get the URL of this category
$category_link = get_category_link( $category_id );
?>

<a href=”<?php echo esc_url( $category_link ); ?>” title=”Category Name”>Category Name</a>[/pre]

The drawback of this method is that you need to specify the category name inside the function. If you want to fetch the category name itself then you need a slightly different approach here.

[space]

Fetching the post Category

To fetch the post category, you need to use something called as get_the_category() function.

[pre]$the_cat = get_the_category();[/pre]

This function returns the current post category if you use it inside a loop. However if you want to use it outside of the loop then you’ll need to pass the post ID as a parameter.

[pre]$the_cat = get_the_category( $id );[/pre]

Once we fetch the categories, we will fetch the category name of the first category in the array.

[pre]$category_name = $the_cat[0]->cat_name;[/pre]

After getting the category name, we simply use the previous code and replace ‘Category Name’ with the variable $category_name.

[pre]$category_id = get_cat_ID( $category_name );
$category_link = get_category_link( $category_id );[/pre]

[space]

Or we can write these two lines in one single line as follows –

[pre]$category_link = get_category_link( $the_cat[0]->cat_ID );[/pre]

[space]

[pre]<?php
$the_cat = get_the_category();
$category_name = $the_cat[0]->cat_name;
$category_link = get_category_link( $the_cat[0]->cat_ID );
?>

 

<a href=”<?php echo $category_link ?>”
title=”<?php echo $category_name ?>”  >
<?php echo $category_name ?>
</a>[/pre]

Place this code inside content.php or single.php or if you want to use it outside of the loop then simply make a small adjustment and add post_id as a parameter inside get_the_category() function and you’re good to go!

Get a FREE Consultation

Let's build something that lasts.

Share what's on your mind — a clear brief, a half-formed idea, or just a sense that something needs to change. We'll listen first, ask the right questions, and point you toward what's actually worth building.

We take on a handful of projects each quarter,ones where we can truly make a difference.

  • Receive a human response within 24 hours
  • Get a detailed scope and quote upfront
  • We're happy to sign an NDA upon request

    Free 30-Min Strategy Call

    Your Name *

    Your Phone No *

    Work Email *

    Your Budget*

    Project Details *