我正在某个网站上做一些工作,并且一切都在Divi中完成。
我只想使用代码为产品页面等构建一些自定义woocommerce模板。但是,当我将模板添加到主题文件夹时,它不会覆盖产品页面。
当我在调试查询中查看时,它显示正在使用et页面构建器模板而不是常规产品模板。
他们的文档都是为非编码者准备的,只有与代码相关的东西才在模块上。
如何从子主题中替换普通模板?
答案 0 :(得分:0)
您只需要确保模板文件的路径正确即可。例如,如果您要覆盖单一产品模板,该模板位于:
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的主题生成器一旦激活,将导致该站点不再使用页面模板。因此,没有办法(缺少重写主题构建器)用您自己的模板文件覆盖它。
但是,您可以自定义主题构建器使用的Divi模块,尽管对其进行编辑有些复杂。
这些模块位于:
wp-content/themes/Divi/includes/builder/module/
例如,我将覆盖WooCommerce标题模块。
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
,以表明该模块已被覆盖。
结果: