You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WordPress条件语句中排除多个页面模板及自定义分类归档页面

How to Exclude Custom Taxonomy Archive Pages Alongside Page Templates

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 named taxonomy-press_category.php—the suffix press_category is the slug of your custom taxonomy, which is what is_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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 18:27:39