Coding my most recent plugin took me an extra day. I was completely lost in getting custom taxonomy term link in the archive pages. I searched for hours, but a perfect answer was nowhere to be found.
I don’t want you guys to waste your time on finding this answer on forums, so here’s how you can get custom taxonomy term link in WordPress archive pages.
First, add following snippet to your current theme’s functions.php file or your plugin:
function get_tax_data($data){ if(!$data) return; global $wp_query; $term = $wp_query->get_queried_object(); if($data == 'title') return $term->name; if($data == 'description') return strip_tags($term->description); if($data == 'link') { $link = get_term_link($term); return $link; } }
Now, you can get the term URL on your archive page using the following code:
<?php echo get_tax_data('link'); ?>
Hope it’s working for you!