WooCommerce 如何检查functions.php 中的页面是否为is_shop()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37350704/
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
WooCommerce how to check if page is_shop() in functions.php?
提问by kd patel
In WooCommerce, My Category Listing page and product listing page are rendered from archieve-product.php ( By Default) . How to check if page is_shop()in functions.php? As is_shop function does not work in functions.php. I simply want to remove my sidebar from Category listingpage not from product listing page.
在 WooCommerce 中,我的类别列表页面和产品列表页面从 archieve-product.php(默认)呈现。如何检查是否页is_shop()中的functions.php?由于 is_shop 函数在functions.php 中不起作用。我只是想从类别列表页面而不是产品列表页面中删除我的侧边栏。
采纳答案by Mukesh Ram
You can write a condition into "archive-product.php"for category page like,
您可以将条件写入 “archive-product.php”以用于类别页面,例如,
$cate = get_queried_object();
if(is_product_category() && $cate->parent != 0 ){
// Write code here
//include sidebar here
}
By using this code this will check the page for product_category and also check for a parent.
通过使用此代码,这将检查 product_category 的页面并检查父级。
回答by Anand Shah
When placed inside a hook, is_shop
will work in functions.php
当放置在钩子内时,is_shop
将在functions.php中工作
add_action( 'template_redirect', 'custom_template_redirect' );
function custom_template_redirect() {
if( is_shop() ) :
// code logic here
endif;
}
Here is a list of all WooCommerce conditionals
这是所有WooCommerce 条件的列表
回答by Ibrahim Hajjaj
call it using WordPress Hook pre get posts
使用 WordPress Hook pre get posts 调用它
add_action('pre_get_posts','nameTheFunction');
function nameTheFunction(){
if(is_shop()){
// your code here
}
}// function end here
Read more about pre get posts Hook
阅读有关 pre get 帖子的更多信息 Hook
https://developer.wordpress.org/reference/hooks/pre_get_posts/
https://developer.wordpress.org/reference/hooks/pre_get_posts/
回答by Cfreak
You can use function_exists
您可以使用 function_exists
if( function_exists("is_shop") ) {
// call it or do something else
}
else {
// load it from somewhere
}
Official docs: https://secure.php.net/function_exists