Through a series of specific requirements, I find myself needing to link to a custom taxonomy category using its term id…
I’ve got this – which displays a link to all taxonomies – I wish to change it so it only displays a link to the taxonomy with the term id dynamically pulled from a custom field I’m using.
$taxonomy = 'event-categories';
$terms = get_terms($taxonomy);
if ($terms) {
foreach($terms as $term) {
echo '<li><a href="http:/mysite.com/events/categories/project-events/' . $term->slug . '">' . $term->name .'</a></li>';
}
};
essentiall I need “link_to_taxonomy_category(x)” where x = term_id
Thanks
The function you are looking for is
get_term_link
. It takes either a term object, ID or slug and a taxonomy name and returns a URL to the term landing page.As a side note hard coding the link as you have in the example above is fragile — always keep your code as portable as possible. If the site is moved to a different domain, that link will break. WordPress has several functions that generate links dynamically based on the current installation environment.
get_term_link
is one example.From the Codex:
If you have single
term_id
e.g:10
, custom taxonomyseries
then you can use the following code to get the taxonomy term link.note : change 10 to your variable for term_id and ‘series’ to your taxonomy.