求助:ACF自定义字段无法在WooCommerce商品详情页指定位置显示
It looks like your code has a couple of critical issues that are stopping the ACF field from showing up correctly. Let’s walk through the fixes step by step:
1. Fix the Incomplete Syntax Error
Your current code cuts off mid-echo statement (echo '<p>&), which is causing a PHP syntax error. This alone would prevent the entire function from running, so that’s the first thing to address.
2. Properly Retrieve and Display the ACF Field
You haven’t actually pulled the value of your ACF pricing table field yet. You need to use ACF’s built-in functions to fetch the content associated with your field.
Corrected Code Snippet
Here’s a revised, working version of your code:
add_action( 'woocommerce_before_add_to_cart_button', 'display_acf_pricing_table_above_add_to_cart', 10 ); function display_acf_pricing_table_above_add_to_cart() { // Replace 'pricing_table' with your actual ACF field name (case-sensitive!) $pricing_table_content = get_field( 'pricing_table' ); // Only display the section if the field has content if ( $pricing_table_content ) { echo '<div id="price-table" style="margin-bottom: 20px;">'; echo '<strong>Price Table:</strong>'; echo $pricing_table_content; // Outputs the ACF field's content echo '</div>'; } }
Key Details to Note:
- Field Name Match: Make sure
'pricing_table'exactly matches the field name you set up in your ACF field group. - Priority Adjustment: Using priority
10ensures the table loads before the add-to-cart button (the button’s default priority is20). - Conditional Check: The
if ( $pricing_table_content )line prevents empty divs from appearing if the field is blank for a specific product. - Spacing: The inline style adds space between the table and button—you can move this to your theme’s CSS for cleaner code.
Extra Troubleshooting Tips
If the table still doesn’t show up:
- Confirm your ACF field group is assigned to the Product post type in ACF’s settings.
- Check your site’s PHP error log for hidden issues (enable debugging temporarily by adding
define('WP_DEBUG', true);to yourwp-config.phpfile). - Test with a simple text ACF field first to rule out issues with the pricing table field type itself.
内容的提问来源于stack exchange,提问作者Joe Bloggs
相关产品推荐
相关产品推荐

