1

I have a custom post type (item) with multiple custom taxonomies (item_category_A, item_category_B, etc.) I'd like to retrieve custom taxonomy terms of the current item from item_category_A, also make them clickable. My code retrieves the correct terms, but then using get_term_link() retrieves a url, looking like sitename.com/item_category_B/category_A_term_slug . What am I doing wrong? My code looks like this:

function get_item_category_A(){

   $item_cat_A = get_the_terms(get_the_ID,'item_category_A');

   echo '<a href="' . esc_url(get_term_link($item_cat_A[0]->slug,$item_cat_A[0]->taxonomy)).'">'.esc_html($item_cat_A[0]->name).'</a>';

}

Still, when I run the function, it returns the url than points to the correct term in the incorrect taxonomy (sitename.com/item_category_B/category_A_term_slug).

2
  • 2
    I would suggest adding the code that registers your taxonomies to your question as well. Might be a rewrite argument askew? Could be a few things.
    – jdm2112
    Apr 15, 2017 at 16:59
  • It was exactly that! Thank you! Please write your comment as an answer so I can accept it.
    – galingong
    Apr 15, 2017 at 17:06

1 Answer 1

1

WordPress allows for customization of the default URL rewrite behavior (/slug/term) with a rewrite arguments array passed to the register_taxonomy() function.

By defining values for the slug or hierarchical keys in this array, the default URL structure can be altered to suit your needs. It may also be altered such that unexpected results are returned.

When creating multiple taxonomies, it is easy to copy & paste the labels and parameters arrays for one tax and overlook the necessity to update some of the specifics for subsequent uses.

WP Codex reference: https://codex.wordpress.org/Function_Reference/register_taxonomy#Arguments

Hierarchical taxonomy explanation via Milo: How to enable hierarchical permalinks for hierarchical taxonomies

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.