【发布时间】:2015-11-16 00:27:46
【问题描述】:
我有一个问题:有没有办法通过插件覆盖 WooCommerce 的默认模板,就像使用主题一样? 我有这个代码:
Class WoocommerceOverride {
public function woocommerce_locate_template( $template, $template_name, $template_path ) {
$plugin_path = SPSL_PLUGIN_PATH;
global $woocommerce;
$_template = $template;
if ( ! $template_path ) $template_path = $woocommerce->template_url;
$plugin_path .= '/woocommerce/';
// Look within passed path within the theme - this is priority
$template = locate_template(
array(
$template_path . $template_name,
$template_name
)
);
// Modification: Get the template from this plugin, if it exists
if ( ! $template && file_exists( $plugin_path . $template_name ) )
$template = $plugin_path . $template_name;
// Use default template
if ( ! $template )
$template = $_template;
//echo $template."<br>";
// Return what we found
return $template;
}
}
add_filter( 'woocommerce_locate_template', array('WoocommerceOverride', 'woocommerce_locate_template'), 10, 3 );
这段代码的问题是它只能部分工作。在某些部分它可以工作,而在其他部分则不能。例如,我根本无法自定义archive-product.php。无论我在那里写什么,无论是代码还是纯文本,我都没有得到任何结果。 我将插件文件夹中完全相同的模板文件复制到我的主题文件夹中,它可以工作。但是,因为我需要这个插件,所以我不能走主题路线。
非常感谢。
【问题讨论】:
-
经过大量研究:stackoverflow.com/a/35252520/4689173
woocommerce_locate_template不加载single-product.php模板。
标签: php wordpress woocommerce