php 如何在woocommerce中检查页面是类别还是产品?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24926164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:33:08  来源:igfitidea点击:

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

phpwordpresswoocommerce

提问by user3777827

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

我正在尝试使用woocommerce.php无法正常工作的is_category 。我想根据页面打印标题。

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

如果页面是类别页面,则将打印woocommerce_page_title(),如果页面是产品,则应打印the_title()

And the code I am using in woocommerce.phpis:

我使用的代码woocommerce.php是:

<?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.

但在任何情况下它都是打印the_title()。我认为is_category()不适用于 woocommerce。

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

或者谁能​​告诉 woocommerce 如何打印类别和产品标题?

Any help will be appreciated.

任何帮助将不胜感激。

回答by Shah Rukh

you should use

你应该使用

is_product_category()

instead of

代替

is_category()

回答by Mayur

Try creating a archive-product.phpfile within the theme templates.

尝试archive-product.php在主题模板中创建一个文件。

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

我通常复制woocommerce文件夹中的那个,然后在我创建的那个中添加一个带有“测试”文本的跨度,这样我就可以看到它正在工作。