【问题标题】:WooCommerce how to check if page is_shop() in functions.php?WooCommerce如何检查functions.php中的页面is_shop()是否?
【发布时间】:2016-05-20 15:46:28
【问题描述】:

在 WooCommerce 中,我的类别列表页面和产品列表页面是从 archieve-product.php 呈现的(默认情况下)。如何检查 functions.php 中的页面 is_shop() 是否?由于 is_shop 函数在 functions.php 中不起作用。我只是想从产品列表页面中删除我的侧边栏从类别列表页面而不是产品列表页面。

【问题讨论】:

  • 您的问题是什么?使用该功能非常简单:“在产品存档页面(商店)上时返回 true”.
  • 是的.. 但问题是当我点击特定类别时。产品显示的类别也是从相同的archieve-product.php 呈现的。因此,即使在产品列表页面中,侧栏也会出现。 ...这有什么意义吗:/ ??

标签: php wordpress woocommerce


【解决方案1】:

当放置在钩子中时,is_shop 将在 functions.php 中起作用

add_action( 'template_redirect', 'custom_template_redirect' );

function custom_template_redirect() {

    if( is_shop() ) :

         // code logic here

    endif;    
}

这里是所有WooCommerce conditionals的列表

【讨论】:

  • 这个 sis 应该被接受为正确答案。这个对我有用。非常感谢
【解决方案2】:

使用 WordPress Hook 调用它 pre get posts

add_action('pre_get_posts','nameTheFunction');

function nameTheFunction(){

   if(is_shop()){

    // your code here 

  }

}// function end here

阅读更多关于 pre get posts Hook

https://developer.wordpress.org/reference/hooks/pre_get_posts/

【讨论】:

    【解决方案3】:

    您可以在 "archive-product.php" 中为类别页面写一个条件,例如,

        $cate = get_queried_object();
        if(is_product_category()  && $cate->parent != 0 ){
    
             // Write code here
             //include sidebar here
        }
    

    通过使用此代码,这将检查页面的 product_category 并检查父级。

    【讨论】:

      【解决方案4】:

      你可以使用function_exists

      if( function_exists("is_shop") ) {
          // call it or do something else
      }
      else {
          // load it from somewhere
      }
      

      官方文档:https://secure.php.net/function_exists

      【讨论】:

      • 函数是“is_shop”,所以你的支票是function_exists('is_shop')
      • 很酷,现在对我有用 :) 好吧,但我仍然有问题。我的类别列表页面和产品列表页面是从同一页面 "archieve-product.php" 呈现的。我只想从类别列表页面中删除侧栏。我可以从functions.php 做吗?目前,它已使用上述代码从两个页面中删除。
      • @kdpatel 您应该发布一些代码(可能在另一个问题中),提供有关问题的更多详细信息
      • add_action('init', 'remove_sidebar_woo'); function remove_sidebar_woo(){ if( function_exists("is_shop") ) { // call it or do something else remove_action('woocommerce_sidebar', 'woocommerce_get_sidebar', 10); } else { // load it from somewhere } }
      • 我想你想打电话给is_shop()。我的功能只是确定它是否存在。您确实应该编辑原始帖子,而不是在 cmets 中发布代码。
      猜你喜欢
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-14
      • 1970-01-01
      相关资源
      最近更新 更多