如何修改WooCommerce产品页面包屑标题 将店铺名替换为产品名
解决WordPress产品页顶部店铺名称替换为产品名称的方法
以下方案默认适配WooCommerce搭建的产品站,操作前建议先备份站点数据:
方案1:使用钩子代码修改(推荐,无需修改主题核心文件)
将对应代码添加到当前使用主题的functions.php文件末尾,也可以通过自定义代码片段类插件插入,避免主题更新后代码被覆盖:
- 如果你的主题调用的是WooCommerce原生页面标题接口,用这段代码:
add_filter( 'woocommerce_page_title', 'replace_shop_title_with_product_name', 10, 1 ); function replace_shop_title_with_product_name( $title ) { if ( is_product() ) { global $post; $current_product = wc_get_product( $post->ID ); if( $current_product ) { $title = $current_product->get_name(); } } return $title; }
- 如果上述代码不生效,说明主题自定义了标题接口,换通用页面标题钩子:
add_filter( 'the_title', 'replace_shop_title_with_product_name', 10, 2 ); function replace_shop_title_with_product_name( $title, $post_id ) { if ( is_product() && $post_id == get_queried_object_id() && in_the_loop() ) { $current_product = wc_get_product( $post_id ); if( $current_product ) { $title = $current_product->get_name(); } } return $title; }
方案2:直接修改主题模板文件
如果代码方案无效,你可以手动定位主题的标题输出模板:
- 一般标题输出逻辑存放在
header.php、page-title.php或者主题自定义的WooCommerce模板片段中 - 找到输出店铺名称的代码段,替换为下方逻辑即可:
if ( is_product() ) { global $product; echo esc_html( $product->get_name() ); } else { // 此处保留原主题其他页面的标题输出代码即可 }
注:修改模板文件前请先备份原文件,出现报错可以快速回滚
内容的提问来源于stack exchange,提问作者İsa Çılgın
相关产品推荐
相关产品推荐

