templates - Avoid taxonomy-%term%.php if more than one taxonomy

admin2025-06-06  8

I have a site with multiple custom taxonomies which are used for filtering posts.

One of the taxonomies has a taxonomy-%term%.php template file.

/?country=the_country shows the country taxonomy template

but /?topic=the_topic&country=the_country uses the country taxonomy template as well.

Is there a simple way to avoid loading the taxonomy-country.php template if more than one taxonomy is being queried?

I have a site with multiple custom taxonomies which are used for filtering posts.

One of the taxonomies has a taxonomy-%term%.php template file.

/?country=the_country shows the country taxonomy template

but /?topic=the_topic&country=the_country uses the country taxonomy template as well.

Is there a simple way to avoid loading the taxonomy-country.php template if more than one taxonomy is being queried?

Share Improve this question asked Nov 7, 2017 at 20:08 admcfajnadmcfajn 1,3262 gold badges13 silver badges30 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

See: Template Hierarchy

My suggestion would be to move away from term-specific template files in this case, and instead use only taxonomy.php, where in that file you can work out the logic you need to in order to render the proper output; i.e., you can then detect the use of multiple taxonomies and adjust as necessary.

The solution here was found by (as jaswrks has mentioned) removing the taxonomy-country.php file entirely.

The contents of taxonomy-country.php are then loaded conditionally within taxonomy.php

Custom post_types & taxonomies have an option to "rewrite" the urls. In this case, the same url parameters that are filtering the taxonomy archives were sometimes colliding with this url-schema and causing unwanted results.

The simple answer would be to disable rewriting the urls of the problem taxonomies.

But a cleaner option turned out to be to

remove_filter( 'template_redirect', 'redirect_canonical' );

... which can be used to disable the rewrite attribute from custom post_types or taxonomies

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749173060a316976.html

最新回复(0)