Wordpress WooCommerce插件模板覆盖
我正在开发一个WooCommerce插件(实际上是普通的WP插件,但只有在启用WooCommerce时才起作用),它应该能够改变标准的WooCommerce输出逻辑。特别是,我需要自己重写标准的archive-product.php模板。Wordpress WooCommerce插件模板覆盖,wordpress,plugins,themes,woocommerce,Wordpress,Plugins,Themes,Woocommerce,我正在开发一个WooCommerce插件(实际上是普通的WP插件,但只有在启用WooCommerce时才起作用),它应该能够改变标准的WooCommerce输出逻辑。特别是,我需要自己重写标准的archive-product.php模板。 我发现在主题中更改模板没有问题,但在插件中无法更改模板。如何在不改变WP和WooCommerce内核的情况下实现这一点?我认为您需要通过WooCommerce可用的挂钩(过滤器和操作)实现这一点 以下是一份清单: 以下是开始使用钩子的地方: 这是我尝试的东西
我发现在主题中更改模板没有问题,但在插件中无法更改模板。如何在不改变WP和WooCommerce内核的情况下实现这一点?我认为您需要通过WooCommerce可用的挂钩(过滤器和操作)实现这一点 以下是一份清单: 以下是开始使用钩子的地方:
这是我尝试的东西。希望这会有帮助 将此筛选器添加到插件中:
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;
}
我检查这个主题的第一次加载。如果在主题中找不到该文件,则将从插件加载该文件
你可以在这里改变逻辑
希望它能帮到你
谢谢