• My Google fu has failed me. I’m not a PHP guy, and pretty much rely on dissecting code samples in the codex, but I can’t seem to get the get_term_link(); function working for me on a single post view.

    I have a custom taxonomy on a custom post type. I want to link to other custom posts that have the same taxonomical value. No idea. I just want to link to the taxonomy archive where the term is as it is on the current post (so I think ID should work here).

    This is how easy I think it should be (where series is the name of the custom taxonomy):

    get_term_link($post->ID, 'series');

    I feel like this should be a simple thing, and I’m probably being a n00b here.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi,

    Here’s the usage for get_term_link according to its Codex page:

    get_term_link( $term, $taxonomy );

    For the first parameter, it should be the term ID or term slug, not the post ID.

    Please see the Codex page for more information:
    http://codex.ww.wp.xz.cn/Function_Reference/get_term_link

    Let us know if that solves your issue.

    Thread Starter Arley McBlain

    (@arleym)

    I can hardcode the taxonomy, but the term ID needs to be dynamic. Is there a $term->ID magic back-endy thing that will do this?

    Do you just want to link to the terms as explained here?
    http://codex.ww.wp.xz.cn/Taxonomies#Listing_the_terms
    That might be easier to use for what you need than get_term_link.

    If you want to get the term ID of the post, you can use wp_get_post_terms:
    http://codex.ww.wp.xz.cn/Function_Reference/wp_get_post_terms

    Thread Starter Arley McBlain

    (@arleym)

    OK Wow. This is really frustrating to discover, but I’m glad it’s solved. My trial-and-error code was probably working sooner – the big issue was that adding the taxonomy from the post view wasn’t actually adding the taxonomy (same issue on tags and categories). The culprit was the plugin WPML (which may have been out of date). So – the reason I wouldn’t see the current link is that there was no taxonomy set. That’s maddening. I really should have noticed that sooner, but didn’t think to look for it.

    I’ll share the code that worked for future generations:

    $terms = wp_get_post_terms( $post->ID, 'series');
    foreach($terms as $term) {
              echo "More from this Series: <a href='" . get_term_link($term) . "' title='" . $term->name . "'>" . $term->name . "</a>";
    }

    Thanks for the update. Glad it all worked out.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Get Term Link for current post’ is closed to new replies.