简体   繁体   English

如何覆盖woocommerce产品图像模板

[英]How to override woocommerce product-image template

I want to use my own custom template file (with a custom name) for product image and gallery thumbnails, so need to override the default Woocommerce product-image template by using this: 我想将自己的自定义模板文件(带有自定义名称)用于商品图片和画廊缩略图,因此需要使用以下方法覆盖默认的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;
}

But no success yet. 但是还没有成功。 Is there any other way for doing this? 还有其他方法吗?

It's a problem of path that has to be the absolute server path using get_theme_file_path() for example. 例如,这是路径问题 ,必须是使用get_theme_file_path()的绝对服务器路径。

Assuming that your custom template file is named my-gallery.include.php and if: 假设您的自定义模板文件名为my-gallery.include.php并且满足以下条件:

1) it's located in your active theme's root directory you will use: 1)它位于活动主题的根目录中,您将使用:

$located = get_theme_file_path('/my-gallery.include.php');

So in your code: 因此,在您的代码中:

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;
}

Code goes in function.php file of your active child theme (or active theme). 代码进入您的活动子主题(或活动主题)的function.php文件中。 Tested and works. 经过测试和工作。

2) it's located inside a your active theme on a woocommerce subfolder , you will use: 2)它位于woocommerce子文件夹上的活动主题内 ,您将使用:

$located = get_theme_file_path('/woocommerce/my-gallery.include.php');

3) If you want to override the template via your active theme , just copy the file from the woocommerce plugin to: woocommerce/single-product/product-image.php 3)如果要通过活动主题覆盖模板 ,只需将文件从woocommerce插件复制到: woocommerce/single-product/product-image.php

Now you can open edit the copied template and make changes without your actual code and the changes will appear once saved. 现在,您可以打开编辑复制的模板,并在没有实际代码的情况下进行更改,更改将在保存后显示。

You can override woocommerce product-image template 您可以覆盖woocommerce产品图像模板

project/wp-content/plugins/woocommerce/templates/single-product/product-image.php

Create below folder structure and copy above page and do modification as per your requirement 创建以下文件夹结构并复制上一页,然后根据需要进行修改

project/wp-content/themes/yourtheme/woocommerce/templates/single-product/product-image.php

To override any woocommerce template you don't need to write any code. 要覆盖任何woocommerce模板,您无需编写任何代码。 You can do is simply creating that file inside your active theme. 您可以做的就是在活动主题内创建该文件。 For Example in your case you want to overwrite 'product-image.php' which is located inside 例如,您要覆盖位于内部的“ product-image.php”

wp-content/plugins/woocommerce/templates/single-product/product-image.php wp-content / plugins / woocommerce / templates / single-product / product-image.php

So for that what you can do, you can just create that template inside your path and for that you need to create the path to the template inside the theme. 因此,为此您可以在路径内创建该模板,并为此需要在主题内创建模板的路径。 Like in your case create the path to the template ie : 就像您的情况一样,创建模板的路径,即:

woocommerce -> templates -> single-product -> product-image.php woocommerce->模板->单一产品-> product-image.php

When woocommerce runs it will take the template from the theme if available else from the plugin. 当woocommerce运行时,它将从主题中获取模板(如果可从插件获取)。

Hope this make sense. 希望这有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM