I am building a taxonomy archive page and I am trying to create a linked instance of the current term outside the loop. So, let's say my taxonomy and current term are:

  • Taxonomy: Animals
  • Term: Elephants

I want to echo the term Elephants and have the term name linked to the Elephants term archive. So, this would be the output:

<a href="http://www.mywebsite.com/tag/elephants/">Elephants</a>

Elsewhere I use single_term_title(); and that perfectly grabs the term name for that specific archive page. In other words, I can echo the title fine. I just want it linked to the archive page for that term. In an ideal world, the code that I want would be:

<a href="http://www.mywebsite.com/tag/<?php single_term_slug(); ?>/"><?php single_term_title(); ?></a>

There is no single_term_slug(); and I have tried a number of things, but so far nothing has worked.

Another alternative is that I am trying to echo the permalink for the current page. I have looked into ways of doing it that was as well. Something like get_canonical(); but that does not exist either.

So either the answer is something that I have not found or could not come up with myself... Or it is so darn easy that it is right in front of my face (which could very well be the case).



It sounds like you're looking for get_term_link( $term_ID ) which takes the term ID and returns a link.


EDIT: Try this:

$term_id = get_query_var('tag_id');
$term_link = get_term_link( $term_id );
| improve this answer | |
  • Thanks for the reply. I tried to use that but it would return the first tag for the top post in the archive and not the term of the archive page. Using my example above where we are on the term archive page for Elephants, let's say the most recent post in the archive was about Mammals and had terms "Badger" and "Elephant". When using get_term_link it returns Badger and not Elephant. I need a piece of code that identifies the current page as a term archive for Elephant and then echos a linked instance of that term. I think get_term_link is the way, I just don't know how to build it. – Ethan May 4 '12 at 15:25
  • The frustrating thing, as I said above is that single_term_title(); returns exactly what I need, except it is the title of the term and not the slug. single_term_slug(); would do the trick but it doesn't exist. Another possible solution would be to use single_term_title(); and then somehow rewrite the title to convert it into the slug. So a term "Bald Eagle" would be rewritten to "bald-eagle". That would work too. But that would also be a bit of a challenge for me given my skill level. – Ethan May 4 '12 at 15:44
  • It sounds like what you need then is to find the ID of the current term archive page so you can use get_term_link(). – mrwweb May 4 '12 at 17:24
  • Yup, that's exactly what I need. Only question is how. – Ethan May 4 '12 at 17:40
  • Got it! Here is the full code for the linked term: <a href="http://www.mydomain.com/tag/<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); echo $term->slug; ?>/"><?php single_term_title(); ?></a> – Ethan May 4 '12 at 18:09

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.