WordPress 2023子主题taxonomy-product-cat.php不生效,仅加载父主题内容
问题描述
我在子主题路径wp-content/themes/yourtheme/woocommerce/templates/taxonomy-product-cat.php中创建并自定义了taxonomy-product-cat.php文件,但当前子主题始终只加载父主题的对应模板内容。该功能在Twenty Twenty-One子主题中可正常运行,且我已按照WooCommerce官方模板结构文档操作。

排查修复步骤
修正模板路径
WooCommerce子主题覆盖模板时不需要嵌套templates目录,正确路径应为wp-content/themes/yourtheme/woocommerce/taxonomy-product-cat.php。父主题的模板存放在woocommerce/templates/下,但子主题覆盖必须去掉templates层级,直接将文件放在子主题的woocommerce根目录下。确认子主题激活状态
确保WordPress后台当前激活的是你修改的目标子主题,而非父主题或其他子主题。核对文件名准确性
确认文件名完全为taxonomy-product-cat.php,注意服务器大小写规则(Linux系统区分文件名大小写),避免拼写错误。清除各类缓存
清理WordPress内置缓存、浏览器缓存,若使用CDN或缓存插件也需同步清理,防止旧模板内容被缓存展示。强制指定子主题模板
在子主题的functions.php中添加以下代码,强制加载子主题的分类模板,测试是否生效:add_filter('template_include', 'force_child_woocommerce_template', 99); function force_child_woocommerce_template($template) { if (is_product_category()) { $child_template = get_stylesheet_directory() . '/woocommerce/taxonomy-product-cat.php'; if (file_exists($child_template)) { return $child_template; } } return $template; }排查父主题特殊逻辑
部分自定义父主题可能通过钩子修改了模板加载规则,导致子主题模板被忽略。可以临时切换到Twenty Twenty-One父主题,测试子主题模板是否正常加载,以此判断是否为当前父主题的问题。
内容的提问来源于stack exchange,提问作者Znaneswar

