您只需确保模板文件的路径正确。例如,如果您尝试覆盖 single-product 模板,该模板位于:
wp-content/plugins/woocommerce/templates/single-product.php
只需复制到:
wp-content/themes/{your-child-theme}/woocommerce/single-product.php
并在那里进行更改。
对于任何模板文件,只需匹配路径减去templates 文件夹即可。
如果您安装了任何缓存插件,您可能需要在更改显示之前清除缓存。
为了测试它,我复制了single-product.php 和product-image.php 并进行了以下更改。
你可以在这里看到结果:
如果这对您不起作用,请确保您的子主题设置正确。
编辑: Divi 的主题生成器一旦激活,就会导致网站不再使用页面模板。所以没有办法(除了重写主题生成器)用你自己的模板文件覆盖它。
但是,您可以自定义 Theme Builder 使用的 Divi 模块,尽管编辑它们有点复杂。
模块位于:
wp-content/themes/Divi/includes/builder/module/
例如,我将覆盖 WooCommerce Title 模块。
wp-content/themes/Divi/includes/builder/module/woocommerce/Title.php
首先,将该文件复制到您的子主题中并将其放在一个新文件夹中:
wp-content/themes/Divi-child/custom-modules/Title.php
接下来,添加以下代码您的子主题的functions.php 以替换现有模块:
function divi_child_theme_setup() {
if ( class_exists('ET_Builder_Module')) {
get_template_part( 'custom-modules/custom-title' );
$TE_ct = new Custom_ET_Builder_Module_Woocommerce_Title();
remove_shortcode( 'et_pb_wc_title' );
add_shortcode( 'et_pb_wc_title', array($TE_ct, '_shortcode_callback') );
}
}
add_action('wp', 'divi_child_theme_setup', 9999);
随意调用变量 ($TE_ct) 和模块 (Custom_ET_Builder_Module_Woocommerce_Title)。
最后,在您的子主题中编辑模块。确保类名与您在 functions.php 中使用的名称匹配。
...
class Custom_ET_Builder_Module_Woocommerce_Title extends ET_Builder_Module {
/**
* Initialize.
*/
public function init() {
echo "<h1>CUSTOMIZED!!</h1>";
$this->name = esc_html__( 'Woo Title', 'et_builder' );
$this->plural = esc_html__( 'Woo Titles', 'et_builder' );
$this->slug = 'et_pb_wc_title';
$this->vb_support = 'on';
...
在这里,我添加了一个简单的echo 以表明该模块正在被覆盖。
结果: