Can anyone help me with Wordpress's get_term_link()?

I’m thinking there’s probably a simple question to this or I’m having a brain fart here or something…

So, I want to produce a link in one of my theme files to a certain term, “all” in a taxonomy, “jobs.” I can’t manually put in the link because I don’t know how the client is going to setup their permalink structure.

I found the get_term_link() function in /wp-includes/taxonomy.php and I’ve been trying to use it, as it seems pretty straight forward, but I just can’t get it to work.

I couldn’t find any documentation in the Wordpress Codex on the function, but looking in taxonomy.php it has the function setup like this:

get_term_link( $term, $taxonomy )

So, I assume I should be able to just do this in my theme file:

Back to Jobs Home

(Note: Ignore that ‘<’ and ‘>’ aren showing their HTML equivalent above - TF issue…)

But I get this error message:

Catchable fatal error: Object of class WP_Error could not be converted to string in /path-to-my-theme-file/file.php on line 38

Anyone know what I’m doing wrong?

Hmm, just browsed PHPXref for WP (great resource btw), and saw this in the loop.php file of TwentyTen:

View it on TinyPaste

So I think you’re missing one argument?

The get_term_link($term, $taxonomy) function takes two parameters:

  1. $term: This can be one of three data types: String (term name or slug), Integer (term id) or Object (term object).
  2. $taxonomy: This is the taxonomy name, which should already be defined with register_taxonomy() if it's not a built-in taxonomy, such as category or post_tag.

The WP_Error is thrown when either the term specified does not exist within the taxonomy, or the taxonomy has not been defined.

To have a better idea and hopefully fix your problem, it is good to see the list of taxonomies available, by using the get_terms($taxonomy). It will return an array of Taxonomy Objects, which contain the data structure of each taxonomy. For example:

    [0] => stdClass Object
            [term_id] => 37
            [name] => Beach
            [slug] => beach
            [term_group] => 0
            [term_taxonomy_id] => 56
            [taxonomy] => portfolio-category
            [description] => 
            [parent] => 0
            [count] => 7

In the example I’m using a custom taxonomy I’ve created previously, called ‘portfolio-category’. Now, based on the information provided by the taxonomy object, you can get the link in 3 different ways:

  • By using the taxonomy name: get_term_link('Beach', 'portfolio-category');
  • By using the taxonomy slug: get_term_link('beach', 'portfolio-category');
  • By using the taxonomy object: get_term_link($term, 'portfolio-category');

Last, but not least… To get a comma separated list of taxonomies, you can use the following code, assuming that you have the $post_id:

$terms = get_the_term_list($post_id, 'portfolio-category', null, ', ', null);

Hope this helps!

Thanks guys for your help, guys! I knew I was just having a brain fart, I was putting in my post type, not my taxonomy… so stupid. It’s too early. I need to go home.