Skip to content

Instantly share code, notes, and snippets.

@DanBeckett
Created June 17, 2014 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanBeckett/e307c43b201646ccf9b9 to your computer and use it in GitHub Desktop.
Save DanBeckett/e307c43b201646ccf9b9 to your computer and use it in GitHub Desktop.
Quick function to convert a permalink into a term object when inside a custom taxonomy
function get_term_object_from_uri($taxonomy) {
global $wp;
//get requested uri
$wp_request = $wp->request;
//split into array
$uri_params = explode('/', $wp_request);
//drop the product id as unnecessary
$param_total = (count($uri_params)-1);
unset($uri_params[$param_total]);
//get the new deepest level category
$param_top_level = $param_total-1;
$param_slug = $uri_params[$param_top_level];
$param_object = get_term_by( 'slug', $param_slug, $taxonomy );
return $param_object;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment