【问题标题】:WooCommerce plugin template overridingWooCommerce 插件模板覆盖
【发布时间】:2013-02-26 00:57:58
【问题描述】:

我正在开发一个 WooCommerce 插件(实际上是常用的 WP 插件,但仅在启用 WooCommerce 时才有效),它应该改变标准的 WooCommerce 输出逻辑。特别是我需要自己覆盖标准的 archive-product.php 模板。 我发现在主题中更改模板没有问题,但在插件中无法做到这一点。我如何对 WP 和 WooCommerce 核心进行任何更改的情况下做到这一点?

【问题讨论】:

    标签: wordpress plugins themes woocommerce


    【解决方案1】:

    我认为您需要通过 WooCommerce 可用的钩子(过滤器和操作)来做到这一点。

    这是一个列表: http://docs.woothemes.com/document/hooks/#templatehooks

    这是开始使用钩子的地方: http://wp.tutsplus.com/tutorials/the-beginners-guide-to-wordpress-actions-and-filters/

    【讨论】:

      【解决方案2】:

      这是我尝试这样的事情。希望它会有所帮助。

      将此过滤器添加到您的插件中:

      add_filter( 'template_include', 'my_include_template_function' );
      

      然后回调函数会是

      function my_include_template_function( $template_path ) {
      
                  if ( is_single() && get_post_type() == 'product' ) {
      
                      // checks if the file exists in the theme first,
                      // otherwise serve the file from the plugin
                      if ( $theme_file = locate_template( array ( 'single-product.php' ) ) ) {
                          $template_path = $theme_file;
                      } else {
                          $template_path = PLUGIN_TEMPLATE_PATH . 'single-product.php';
                      }
      
                  } elseif ( is_product_taxonomy() ) {
      
                      if ( is_tax( 'product_cat' ) ) {
      
                          // checks if the file exists in the theme first,
                          // otherwise serve the file from the plugin
                          if ( $theme_file = locate_template( array ( 'taxonomy-product_cat.php' ) ) ) {
                              $template_path = $theme_file;
                          } else {
                              $template_path = PLUGIN_TEMPLATE_PATH . 'taxonomy-product_cat.php';
                          }
      
                      } else {
      
                          // checks if the file exists in the theme first,
                          // otherwise serve the file from the plugin
                          if ( $theme_file = locate_template( array ( 'archive-product.php' ) ) ) {
                              $template_path = $theme_file;
                          } else {
                              $template_path = PLUGIN_TEMPLATE_PATH . 'archive-product.php';
                          }
                      }
      
                  } elseif ( is_archive() && get_post_type() == 'product' ) {
      
                      // checks if the file exists in the theme first,
                      // otherwise serve the file from the plugin
                      if ( $theme_file = locate_template( array ( 'archive-product.php' ) ) ) {
                          $template_path = $theme_file;
                      } else {
                          $template_path = PLUGIN_TEMPLATE_PATH . 'archive-product.php';
                      }
      
                  }
      
              return $template_path;
          }
      

      我检查这个主题是否首次加载。如果在主题中找不到该文件,则将从插件加载。

      您可以在此处更改逻辑。

      希望它能完成你的工作。

      谢谢

      【讨论】:

        猜你喜欢
        • 2015-11-16
        • 2018-06-03
        • 2013-02-08
        • 2020-08-03
        • 2017-01-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多