WooCommerce产品页自定义内容意外显示在分类页问题求助
问题与解决:WooCommerce自定义内容错误出现在分类页
问题场景
在functions.php中通过woocommerce_short_description钩子添加了产品自定义PDF字段和联系按钮,期望仅在单产品页短描述下方显示,但后台给分类页添加描述后,该内容会错误显示在分类页产品列表上方。尝试其他单产品页钩子无法正常显示内容,仅该钩子能生效但带来分类页的问题。
错误原因
woocommerce_short_description钩子的作用范围不仅限于单产品页的产品短描述,还会作用于分类页的分类描述(当分类设置了描述时)。代码中没有判断当前页面类型,导致分类页调用该钩子时也执行了自定义内容的追加逻辑;同时直接调用global $product,在分类页环境下该对象可能无效,还可能触发PHP警告。
修复方案
在函数开头添加页面类型判断,确保仅在单产品详情页下执行代码,同时验证$product对象的有效性。
修复后的代码
/* Show PDF link, PDF name and Contact Us on product pages */ add_filter( 'woocommerce_short_description', 'showpdflink', 3 ); function showpdflink($description){ // 仅在单产品页执行,且确保$product对象有效 if ( !is_product() || !function_exists('wc_get_product') ) { return $description; } global $product; // 验证产品对象 if ( !is_a($product, 'WC_Product') ) { return $description; } $link1=''; // Get the custom field value $mypdfcol = get_post_meta( $product->get_id(), 'cop_pdflink', true ); $mypdfnam = get_post_meta( $product->get_id(), 'cop_pdfname', true ); // Display //Build the links for the pdeffers if( ! empty($mypdfcol) ){ $link1='<hr /> <div class="pdflink"> <div class="et_pb_row et_pb_row_13 et_pb_equal_columns"> <div class="et_pb_column et_pb_column_1_4"><img src="/wp-content/uploads/2022/12/pdf-icon.png" /></div> <div class="et_pb_column et_pb_column_3_4"> <a href="'.$mypdfcol.'" title="Download pdf'; if( ! empty($mypdfnam) ){ $link1=$link1.' for '.$mypdfnam.''; } $link1=$link1.'" target="_blank" rel="noopener">Download pdf'; if( ! empty($mypdfnam) ){ $link1=$link1.' for '.$mypdfnam.''; } if( ! empty($mypdfcol) ){ $link1=$link1.'</a></div> </div> </div>'; } } /* Show CONTACT US button */ $link1=$link1.' <div class="contactbutton"> <a href="/kontakt-os" title="Kontakt CopyColor"> <span class="large">Ring for pris</span><br /> <span class="small">Klik her for kontakt</span> </a> </div>'; /* Show everything on the product page */ return $description.$link1; } /* End of Show PDF link */
替代方案:使用单产品专属钩子
如果更严谨,建议使用woocommerce_single_product_summary钩子,它是WooCommerce专门为单产品页内容区域提供的钩子,不会影响分类页。示例代码如下:
/* Show PDF link, PDF name and Contact Us on product pages */ add_action( 'woocommerce_single_product_summary', 'showpdflink', 35 ); function showpdflink(){ global $product; if ( !is_a($product, 'WC_Product') ) { return; } $link1=''; $mypdfcol = get_post_meta( $product->get_id(), 'cop_pdflink', true ); $mypdfnam = get_post_meta( $product->get_id(), 'cop_pdfname', true ); if( ! empty($mypdfcol) ){ $link1='<hr /> <div class="pdflink"> <div class="et_pb_row et_pb_row_13 et_pb_equal_columns"> <div class="et_pb_column et_pb_column_1_4"><img src="/wp-content/uploads/2022/12/pdf-icon.png" /></div> <div class="et_pb_column et_pb_column_3_4"> <a href="'.$mypdfcol.'" title="Download pdf'; if( ! empty($mypdfnam) ){ $link1=$link1.' for '.$mypdfnam.''; } $link1=$link1.'" target="_blank" rel="noopener">Download pdf'; if( ! empty($mypdfnam) ){ $link1=$link1.' for '.$mypdfnam.''; } $link1=$link1.'</a></div> </div> </div>'; } $link1=$link1.' <div class="contactbutton"> <a href="/kontakt-os" title="Kontakt CopyColor"> <span class="large">Ring for pris</span><br /> <span class="small">Klik her for kontakt</span> </a> </div>'; echo $link1; } /* End of Show PDF link */
注:钩子优先级
35表示内容会显示在短描述(优先级20)和添加到购物车按钮(优先级30)之间,可根据需求调整数值(数值越大,显示位置越靠下)。
内容的提问来源于stack exchange,提问作者Gipa
相关产品推荐
相关产品推荐

