我想使用我自己的自定义模板文件(具有自定义名称)用于产品图片和画廊缩略图,因此需要使用以下方法覆盖默认的Woocommerce产品图片模板:
add_filter( 'wc_get_template', 'modify_product_gallery_template', 10, 5 );
function modify_product_gallery_template( $located, $template_name, $args, $template_path, $default_path ) {
if ( 'single-product/product-image.php' == $template_name ) {
$located = '... my-gallery.include.php';
}
return $located;
}
但是还没有成功。还有其他方法吗?
答案 0 :(得分:2)
例如,这是路径问题,它必须是使用get_theme_file_path()
的绝对服务器路径。
假设您的自定义模板文件名为my-gallery.include.php
,并且如果满足以下条件:
1)它位于活动主题的根目录中,您将使用:
$located = get_theme_file_path('/my-gallery.include.php');
所以在您的代码中:
add_filter( 'wc_get_template', 'modify_product_gallery_template', 10, 5 );
function modify_product_gallery_template( $located, $template_name, $args, $template_path, $default_path ) {
if ( 'single-product/product-image.php' == $template_name ) {
$located = get_theme_file_path('/my-gallery.include.php');
}
return $located;
}
代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。
2)它位于woocommerce
子文件夹上的活动主题内,您将使用:
$located = get_theme_file_path('/woocommerce/my-gallery.include.php');
3)如果要override the template via your active theme ,只需将文件从woocommerce插件复制到:woocommerce/single-product/product-image.php
…
现在,您可以打开编辑复制的模板并进行更改,而无需实际代码,更改将在保存后显示。
答案 1 :(得分:0)
您可以覆盖woocommerce产品图像模板
project/wp-content/plugins/woocommerce/templates/single-product/product-image.php
创建下面的文件夹结构并复制上一页,然后根据您的要求进行修改
project/wp-content/themes/yourtheme/woocommerce/templates/single-product/product-image.php
答案 2 :(得分:0)
要覆盖任何woocommerce模板,您无需编写任何代码。您可以做的就是在活动主题内创建该文件。例如,您要覆盖位于内部的“ product-image.php”
wp-content / plugins / woocommerce / templates / single-product / product-image.php
因此,您可以在路径内创建该模板,并需要在主题内创建该模板的路径。就像您的情况一样,创建模板的路径,即:
woocommerce->模板->单一产品-> product-image.php
运行woocommerce时,它将从主题中获取模板(如果可从插件获取的话)。
希望这很有意义。