WordPress替换Dokan商家店铺页默认页眉/页脚为自定义模板
Dokan商家店铺页替换FSE自定义页眉/页脚解决方案
问题背景
已在全站点编辑(FSE)中创建自定义页眉(模板部件名称:My Custom Header)和页脚,但Dokan商家店铺页的自定义PHP模板无法通过get_header('my_custom_header')/get_footer('my_custom_footer')替换默认页眉页脚。
解决方法
方法1:直接渲染FSE模板部件
FSE模板部件依赖WordPress块系统,而非传统主题的get_header()/get_footer()机制,因此可以用do_blocks()函数直接渲染自定义模板部件:
修改Dokan店铺页模板代码,替换原有的get_header('shop')和get_footer('shop'):
<?php /** * The Template for displaying all single posts. * @package dokan - 2014 1.0 */ if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly } // 渲染自定义页眉模板部件 echo do_blocks( '<!-- wp:template-part {"slug":"my-custom-header","theme":"你的FSE主题slug","area":"header"} /-->' ); ?> <?php do_action( 'woocommerce_before_main_content' ); ?> <div class="dokan-store-wrap .... .... </div><!-- .dokan-store-wrap --> <?php do_action( 'woocommerce_after_main_content' ); ?> <?php // 渲染自定义页脚模板部件 echo do_blocks( '<!-- wp:template-part {"slug":"my-custom-footer","theme":"你的FSE主题slug","area":"footer"} /-->' ); ?>
关键注意事项
- 将
你的FSE主题slug替换为当前使用的FSE主题实际slug(例如twentytwentyfour) - 模板部件
slug规则:名称转小写,空格替换为短横线,比如"My Custom Header"对应my-custom-header - 若不确定slug,可在后台「外观」→「模板部件」中编辑目标部件,查看地址栏或部件详情确认
方法2:移除默认钩子并添加自定义调用
若方法1无效,可通过移除Dokan/WooCommerce的默认页眉页脚钩子,再添加自定义模板部件调用:
在主题的functions.php中添加以下代码:
// 移除默认店铺页页眉 remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 ); // 添加自定义页眉 add_action( 'woocommerce_before_main_content', 'render_custom_fse_header', 5 ); function render_custom_fse_header() { echo do_blocks( '<!-- wp:template-part {"slug":"my-custom-header","theme":"你的FSE主题slug","area":"header"} /-->' ); } // 移除默认店铺页页脚 remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 ); // 添加自定义页脚 add_action( 'woocommerce_after_main_content', 'render_custom_fse_footer', 15 ); function render_custom_fse_footer() { echo do_blocks( '<!-- wp:template-part {"slug":"my-custom-footer","theme":"你的FSE主题slug","area":"footer"} /-->' ); }
同时删除Dokan店铺模板中的get_header('shop')和get_footer('shop')代码。
内容的提问来源于stack exchange,提问作者Ninja
相关产品推荐
相关产品推荐

