【问题标题】:WooCommerce plugin template overridingWooCommerce 插件模板覆盖
【发布时间】:2013-02-26 00:57:58
【问题描述】:
我正在开发一个 WooCommerce 插件(实际上是常用的 WP 插件,但仅在启用 WooCommerce 时才有效),它应该改变标准的 WooCommerce 输出逻辑。特别是我需要自己覆盖标准的 archive-product.php 模板。
我发现在主题中更改模板没有问题,但在插件中无法做到这一点。我如何在对 WP 和 WooCommerce 核心进行任何更改的情况下做到这一点?
【问题讨论】:
标签:
wordpress
plugins
themes
woocommerce
【解决方案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;
}
我检查这个主题是否首次加载。如果在主题中找不到该文件,则将从插件加载。
您可以在此处更改逻辑。
希望它能完成你的工作。
谢谢