Why is_category() Fails in WooCommerce and How to Detect Page Types Correctly
In WooCommerce, distinguishing between a category archive page and a single product page is a common requirement for theme developers. The standard WordPress function is_category() often fails to trigger as expected within the context of WooCommerce templates like woocommerce.php. This behavior stems from how WooCommerce handles template hierarchy and conditional loading, which differs significantly from standard WordPress core logic.
When you wrap your title logic in an if (is_category()) block inside woocommerce.php, the condition evaluates to false even when viewing a category archive. The root cause is that woocommerce.php is not always rendered directly for category pages. Instead, WooCommerce relies on its specific template hierarchy to locate the correct file. For category archives, WooCommerce looks for files in the following order:
archive-{category-slug}.phparchive-product.php(Note: Categories are technically treated as products in this context)woocommerce.php
If a specific archive template exists, woocommerce.php is bypassed entirely for that specific request type. However, if you are customizing the global fallback or dealing with single product views, the detection logic must align with WooCommerce's internal flags rather than generic WordPress post types.
Using WooCommerce's Built-in Conditions
The most reliable way to detect the current view in a WooCommerce environment is to use functions provided by the WooCommerce class itself. Instead of relying solely on is_category(), you should check for specific WooCommerce conditional tags that are designed to work within their ecosystem.
To handle both categories and single products, you can utilize is_product() combined with is_archive(). However, a more direct approach for the specific scenario describedâprinting different titles based on whether the current view is an archive (category) or a single itemâis to check for the presence of product data in the query.
Here is the corrected logic pattern:
<?php if ( is_product() ) { ?>
<h1 class="page-title"><?php the_title(); ?></h1>
<?php } elseif ( is_archive() && is_category() ) { ?>
<!-- Note: is_category() works here because we are inside an archive context -->
<h1 class="page-title"><?php woocommerce_page_title(); ?></h1>
<?php } else { ?>
<h1 class="page-title"><?php the_title(); ?></h1>
<?php } ?>
While the snippet above attempts to fix the immediate issue, it relies on is_archive() which might return true for taxonomies other than products. A more robust check specifically for product categories within WooCommerce is often achieved by checking if the current query is a category archive that contains products.
The Correct Approach: Checking Query Context
The original code failed because woocommerce.php acts as a catch-all template. When a user visits a category page, WooCommerce might still load woocommerce.php if no specific archive-*.php exists. In this specific fallback scenario, is_category() technically should work, but often the post type or query structure makes it ambiguous.
A better strategy is to check if the current object is a product archive specifically. You can use the following logic which is less prone to false negatives in complex themes:
<?php
// Check if we are on a single product page
if ( is_single_product() ) {
echo '<h1 class="page-title">' . get_the_title() . '</h1>';
}
// Check if we are on a category archive (product categories)
elseif ( is_category() && is_shop_page() !== true ) { // Exclude shop page logic if needed
echo '<h1 class="page-title">' . woocommerce_page_title() . '</h1>';
}
else {
echo '<h1 class="page-title">' . get_the_title() . '</h1>';
}
?>
The critical distinction here is understanding that woocommerce_page_title() is the standard hook provided by WooCommerce to output the archive title. It automatically handles the taxonomy label and count if configured in settings. Using the_title() directly on a category page often results in duplicate titles or incorrect formatting because the main query's post object might not be set up in the same way as a standard blog post.
Implementing Custom Logic Safely
If you are modifying woocommerce.php globally, consider that this file is also used for single products when no specific template exists. Your conditional logic must account for this overlap. The safest implementation separates concerns by checking the state of the product object first:
<?php
if ( wc_get_container()->get( ProductRepository::class )->get_current_product() ) {
// Logic for single product
} elseif ( is_category() && is_tax() === false ) {
// Logic for category archive
} else {
// Fallback logic
}
?>
However, a simpler and more maintainable solution often involves hooking into the woocommerce_before_main_content action or using a dedicated template file rather than cluttering the global woocommerce.php. By creating an archive-product.php in your theme's root directory (or wp-content/themes/your-theme/woocommerce/), you can override the behavior specifically for category pages without affecting single product views.
In that dedicated archive template, is_category() will behave predictably because the query context is explicitly an archive. This separation of concerns ensures that your title logic remains robust regardless of how many templates WooCommerce decides to load.