【问题标题】:How to override woocommerce product-image template如何覆盖 woocommerce 产品图像模板
【发布时间】:2019-09-06 09:35:29
【问题描述】:

我想为产品图片和画廊缩略图使用我自己的自定义模板文件(带有自定义名称),因此需要使用以下方法覆盖默认的 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;
}

但是还没有成功。有没有其他方法可以做到这一点?

【问题讨论】:

    标签: php wordpress templates woocommerce path


    【解决方案1】:

    这是路径问题,它必须是使用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...

    现在您可以打开编辑复制的模板并在没有实际代码的情况下进行更改,并且更改将在保存后显示。

    【讨论】:

    • 很好 - 不知道使用 get_theme_file_path 代替 get_stylesheet_directory / get_stylesheet_directory_uri
    • @Stender 首先,get_stylesheet_directory_uri() 仅适用于子主题并且不提供服务器路径,而是一个 URL,它不适用于此钩子中的模板位置。
    【解决方案2】:

    要覆盖任何 woocommerce 模板,您无需编写任何代码。您可以做的只是在您的活动主题中创建该文件。例如,在您的情况下,您想覆盖位于内部的“product-image.php”

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

    为此,您可以在路径中创建该模板,为此您需要在主题中创建模板的路径。就像您的情况一样,创建模板的路径,即:

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

    当 woocommerce 运行时,它将从主题中获取模板(如果插件中的其他模板可用)。

    希望这是有道理的。

    【讨论】:

    • 感谢@Malaya,但在我的情况下,我需要在我的插件中覆盖而不是在主题中。为此需要使用哪个钩子?
    • 好的。因此,您可以改用以下代码:function myplugin_woocommerce_locate_template( $template, $template_name, $template_path ) { global $woocommerce; if ( 'single-product/product-image.php' == $template_name ) { $located = '... my-gallery.include.php'; } return $located; } add_filter('woocommerce_locate_template', myplugin_woocommerce_locate_template', 10, 3);
    【解决方案3】:

    您可以覆盖 woocommerce 产品图像模板

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

    创建下面的文件夹结构并复制上面的页面并根据您的要求进行修改

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

    【讨论】:

      猜你喜欢
      • 2020-10-13
      • 1970-01-01
      • 2018-04-18
      • 2014-08-07
      • 2020-08-03
      • 1970-01-01
      • 1970-01-01
      • 2016-08-07
      • 1970-01-01
      相关资源
      最近更新 更多