user3777827
user3777827

Reputation: 344

How to check if a page is category or product in woocommerce?

I'm trying to use is_category in woocommerce.php which is not working. I want to print the title according the page.

IF a page is category page then will print woocommerce_page_title() and if a page is product then should print the_title().

And the code I am using in woocommerce.php is:

<?php if(is_category()){ ?>                       
    <h1 class="page-title"><?php woocommerce_page_title(); ?></h1> 
<?php }
else{
    the_title();
}
?>

But in every case it is printing the_title() . I think is_category() is not working for woocommerce.

Or Can any one tell how woocommerce do it to print category and product title?

Any help will be appreciated.

Upvotes: 18

Views: 69872

Answers (3)

Atif Tariq
Atif Tariq

Reputation: 2772

More Concisely

if( is_product_category( array( 5, 10, 891 ) ) ) {

// do something for product categories with ID = 5 or 10 or 891


} elseif( is_product_category( 'snowboards' ) ) {
// do something only for product categories with slug or title "snowboards"

} else {

// do something else for other pages

}

As product categories are taxonomies. So, here is the list of WordPress native conditional tags we can use as well:

is_tax( 'product_cat' ) – it is an equivalent to is_product_category(),
is_tax( 'product_cat', $category ) – the same story…

This is the example, similar to above:

if( is_tax( 'product_cat', array( 5, 10, 891 ) ) ) {

} elseif( is_tax( 'product_cat', 'snowboards' ) ) {

} else {

}

The reasonable question is – “What is the difference?”

There is no difference, the function is_product_category() is just a wrapper for is_tax( 'product_cat' ).

Upvotes: 3

Mayur
Mayur

Reputation: 3

Try creating a archive-product.php file within the theme templates.

I usually copy the one that is in the woocommerce folder and add a span with "test" text to the one I create so I can see it's working.

Upvotes: -4

Shah Rukh
Shah Rukh

Reputation: 3156

you should use

is_product_category()

instead of

is_category()

Upvotes: 64

Related Questions