php - 如何覆盖 woocommerce 产品图像模板
问题描述
我想为产品图像和画廊缩略图使用我自己的自定义模板文件(具有自定义名称),因此需要使用以下方法覆盖默认的 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;
}
但还没有成功。有没有其他方法可以做到这一点?
解决方案
这是一个路径问题,例如必须是绝对服务器路径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)如果您想通过您的活动主题覆盖模板,只需将文件从 woocommerce 插件复制到:woocommerce/single-product/product-image.php
...</p>
现在您可以打开编辑复制的模板并在没有实际代码的情况下进行更改,并且更改将在保存后出现。
推荐阅读
- sql - Pivot multiple rows in initial table
- jquery - 将其他脚本用于引导模式时,数据表的功能将被禁用
- mongodb - mongodb 上限集合从偏移量消耗
- python - 在没有 NLTK 的情况下删除文本文件中的停用词
- windows - 路径太长_截断为 1024 个字符_路径无法编辑
- docker - Can't push image to google container registry - Caller does not have permission 'storage.buckets.get'
- javascript - ReactiveX: filter out numbers from observable so that total never goes below zero
- javascript - fieldChanged() method not called when "field.property" is changed from child component
- javascript - jsPDF-AutoTable “doc.autoTable 不是函数”
- asp.net - could not load file or assembly Nlog.web.aspnetcore or one of its dependencies