The get_taxonomy_template() WordPress PHP function retrieves the path of a custom taxonomy term template in the current or parent template.
Usage
get_taxonomy_template()
Example:
$template_path = get_taxonomy_template(); echo 'Taxonomy Template Path: ' . $template_path;
Parameters
- None
More information
See WordPress Developer Resources: get_taxonomy_template()
Examples
Display the path of the taxonomy template
Display the path of the current taxonomy template being used.
$template_path = get_taxonomy_template(); echo 'Taxonomy Template Path: ' . $template_path;
Load a specific taxonomy template
Load a specific taxonomy template if it exists, otherwise, load the default taxonomy.php.
if (get_taxonomy_template()) { get_template_part('taxonomy', 'location'); } else { get_template_part('taxonomy'); }
Filter the taxonomy template path
Filter the taxonomy template path using the ‘taxonomy_template’ hook.
add_filter('taxonomy_template', 'change_taxonomy_template_path'); function change_taxonomy_template_path($template) { return str_replace('taxonomy-', 'my-taxonomy-', $template); }
Check if a custom taxonomy template exists
Check if a custom taxonomy template exists for a given term.
$term = get_queried_object(); $template = locate_template('taxonomy-' . $term->taxonomy . '-' . $term->slug . '.php'); if (!empty($template)) { echo 'Custom taxonomy template exists!'; } else { echo 'No custom taxonomy template found.'; }
Create a fallback for the taxonomy template
Create a fallback for the taxonomy template by checking if a custom template exists, otherwise using the default one.
$term = get_queried_object(); $templates = [ 'taxonomy-' . $term->taxonomy . '-' . $term->slug . '.php', 'taxonomy-' . $term->taxonomy . '.php', 'taxonomy.php' ]; $template = locate_template($templates); if (!empty($template)) { echo 'Using the template: ' . $template; } else { echo 'Using the default taxonomy template.'; }