link to custom taxonomy by id
Asked Answered
V

2

8

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

Vortumnus answered 11/4, 2012 at 12:31 Comment(0)
H
16

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:

$terms = get_terms('species');
echo '<ul>';
foreach ($terms as $term) {
    echo '<li><a href="'.get_term_link($term->slug, 'species').'">'.$term->name.'</a></li>';
}
echo '</ul>';
Hluchy answered 11/4, 2012 at 12:49 Comment(8)
Hi, thanks - wI have actually tried that, but couldb't get it to work, i'll have another go now. Hard code is just in there for clarity in the question...Vortumnus
yeah - can't really figue out how to use this - tried $terms = get_terms('event-categories'); echo '<ul>'; foreach ($terms as $term) { echo '<li><a href="'.get_term_link($term->slug, '20').'">'.$term->name.'</a></li>'; } echo '</ul>'; and got the following error "Catchable fatal error: Object of class WP_Error could not be converted to string in..."Vortumnus
The second argument for get_term_link should be the name of the taxonomy ('event-categories' in this case). It'll return an object of class WP_Error if the term or taxonomy doesn't exist. You can catch the value of the error message by saving it to a variable (i.e. $term_link) and examining it before echo'ing: if ( is_wp_error($term_link) ) echo $term_link->get_error_message();Hluchy
I think I might be out of my depth here - not really sure how to use thid - sorry!Vortumnus
$terms = get_terms('event-categories'); echo '<ul>'; foreach ($terms as $term) { echo '<li><a href="'.get_term_link($term->slug, 'event-categories').'">'.$term->name.'</a></li>'; } echo '</ul>';Hluchy
thats great - 'event-categories' is my taxonomy - and this lists all the categories in that taxonomy, I'm trying to list just one of those categories - identitfied by its term idVortumnus
Then don't loop over the terms. If you have a term ID, all you need is: get_term_link( $term_id, 'event-categories' );. That will return the URL for the specific term you are looking for in the 'event-categories' taxonomy.Hluchy
Sorry this just isn't working for me -must be a bug in my taxonomies as I'm fairly certain this is the right method - I've ended up working around in a plugin specific way - one that doesn;t really answer the question - thanks for your help though...Vortumnus
M
6

If you have single term_id e.g: 10, custom taxonomy series 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.

$term = get_term( 10, 'series' );
$term_link = get_term_link( $term );
echo '<a href="' . $term_link . '">View All</a>';
Meanwhile answered 31/8, 2016 at 22:2 Comment(2)
this only show default category and not custom taxonomy.Katheleenkatherin
in the second parameter of the get_term() pass the taxonomy name.Meanwhile

© 2022 - 2024 — McMap. All rights reserved.