2013-03-10 150 views
0

我正在开发一个WooCommerce插件(实际上是通常的WP插件,但只在启用WooCommerce时才起作用),它应该改变标准的WooCommerce输出逻辑。特别是我需要自己重写标准的archive-product.php模板。 我发现在主题中更改模板没有问题,但不能在插件中如何执行。我可以怎么做没有 WP & WooCommerce核心的任何变化?WooCommerce插件模板覆盖

回答

0

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

添加此过滤器到你的插件:

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; 
    } 

我检查了这对主题先行加载。如果在主题中找不到该文件,则会从插件中加载该文件。

您可以在此更改逻辑。

希望它会做你的工作。

谢谢