如何在WordPress条件语句中排除多个页面模板及自定义分类归档页面
Got it, let's get this sorted for you. The issue here is that is_page_template() only checks for page-specific templates—it doesn't handle custom taxonomy archive pages like your taxonomy-press_category.php. To fix this, we need to add a separate condition using WordPress's is_tax() function, which is designed specifically to check if we're on a taxonomy archive page.
Here's the modified code you need:
You can either combine the exclusion conditions with a logical && (meaning "neither the page templates nor the taxonomy archive"):
if ( ! is_page_template( array( 'homepage_template.php', 'services_template.php' ) ) && ! is_tax( 'press_category' ) ) { // Your existing code that should run EXCEPT on the excluded pages/taxonomy archive }
Or wrap the excluded conditions in parentheses and negate the whole group (a more concise way to say "exclude any page that matches either condition"):
if ( ! ( is_page_template( array( 'homepage_template.php', 'services_template.php' ) ) || is_tax( 'press_category' ) ) ) { // Same logic as above—choose whichever syntax you find more readable }
Why this works:
is_tax('press_category')targets exactly your custom taxonomy archive because your template file is namedtaxonomy-press_category.php—the suffixpress_categoryis the slug of your custom taxonomy, which is whatis_tax()uses to identify the page.- The logical operators (
&&/||) let us combine the two exclusion rules: we want our code to run only when neither of the excluded conditions is true.
If you ever need to verify the taxonomy slug later, you can temporarily add echo get_query_var('taxonomy'); to your archive template and load the page—it'll print the exact slug you need for is_tax().
内容的提问来源于stack exchange,提问作者wgoodman

