如何让WooCommerce商品标题追加文本的代码仅对指定商品ID生效?
仅对特定商品ID追加WooCommerce商品标题文本
要让你的代码只作用于指定的商品ID,只需在自定义函数中添加商品ID的判断逻辑,具体修改如下:
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 ); function woocommerce_template_single_title_custom(){ // 定义需要生效的商品ID数组,可添加多个ID $target_product_ids = array(123, 456); $current_product_id = get_the_ID(); $additional_text = 'Additional Text'; if (in_array($current_product_id, $target_product_ids)) { // 匹配到目标商品ID时,输出带追加文本的标题 the_title( '<h3 class="product_title entry-title">', $additional_text.'</h3>' ); } else { // 非目标商品,沿用默认标题输出格式 the_title( '<h3 class="product_title entry-title">', '</h3>' ); } } add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title_custom', 5);
说明
- 修改
$target_product_ids数组中的值,替换成你需要生效的商品ID,多个ID用英文逗号分隔 - 当访问的商品ID不在目标数组中时,会保持原有的标题输出逻辑,不会有额外文本追加
内容的提问来源于stack exchange,提问作者Naseer Ahmed Jan
相关产品推荐
相关产品推荐

