Php 如何覆盖woocommerce产品图像模板
我想使用我自己的自定义模板文件(带有自定义名称)制作产品图像和库缩略图,因此需要使用以下内容覆盖默认的Woocommerce产品图像模板:Php 如何覆盖woocommerce产品图像模板,php,wordpress,templates,woocommerce,path,Php,Wordpress,Templates,Woocommerce,Path,我想使用我自己的自定义模板文件(带有自定义名称)制作产品图像和库缩略图,因此需要使用以下内容覆盖默认的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
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;
}
但是还没有成功。还有其他方法吗?您可以覆盖woocommerce产品图像模板
project/wp-content/plugins/woocommerce/templates/single-product/product-image.php
创建下面的文件夹结构,复制上面的页面,并根据您的要求进行修改
project/wp-content/themes/yourtheme/woocommerce/templates/single-product/product-image.php
这是路径问题,例如,必须是绝对服务器路径
假设您的自定义模板文件名为
my gallery.include.php
,如果:
1)它位于活动主题的根目录中您将使用:
$located = get_theme_file_path('/my-gallery.include.php');
$located = get_theme_file_path('/woocommerce/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('/my-gallery.include.php');
$located = get_theme_file_path('/woocommerce/my-gallery.include.php');
3)如果您想,只需将文件从woocommerce插件复制到:woocommerce/single product/product image.php
现在,您可以打开“编辑复制的模板”并在不使用实际代码的情况下进行更改,这些更改将在保存后显示。要覆盖任何woocommerce模板,您无需编写任何代码。您可以做的只是在活动主题内创建该文件。例如,在您的案例中,您希望覆盖位于内部的“product image.php” wp content/plugins/woocommerce/templates/single product/product-image.php 为此,您可以在路径中创建模板,为此,您需要在主题中创建模板的路径。与您的情况类似,创建模板的路径,即: woocommerce->templates->single product->product-image.php 当woocommerce运行时,它将从主题中获取模板(如果可用),或者从插件中获取模板
希望这有意义 尼斯-不知道如何使用
get\u-theme\u-file\u-path
而不是get\u-stylesheet\u-directory\u-uri
@Stender首先,get\u-stylesheet\u-directory\u-uri()
只对子主题有效,不提供服务器路径,而是URL,这对这个钩子中的模板位置不起作用。谢谢@Malaya,但在我的情况下,我需要在插件中覆盖,而不是在主题中。要用哪个钩子?好的。因此,您可以使用以下代码:function myplugin\u woocommerce\u locate\u template($template,$template\u name,$template\u path){global$woocommerce;if('single product/product image.php'==$template\u name){$located='…my gallery.include.php'}返回$located;}添加过滤器('woocommerce_locate_template',myplugin_woocommerce_locate_template',10,3);