Support

Account

Home Forums Front-end Issues the_field('field_name') does not work

Helping

the_field('field_name') does not work

  • Hello everyone, I am struggling with an issue to display a custom field value in product attributes table.

    I am running on WordPress 6.0.2 on php 7.3.33 at BlueHost

    I am using a child theme, and have created a document in /childTheme/WooCommerce etc.

    I have modified WC php code to add a new row in the attributes table, but I can’t get the function the_field(‘field_name’) to work. I also tried get_field. Both attempts returns a valid page but without a value.

    This is the code I am using

    defined( 'ABSPATH' ) || exit;
    
    if ( ! $product_attributes ) {
        return;
    }
    ?>
    <table class="woocommerce-product-attributes shop_attributes">
        <?php foreach ( $product_attributes as $product_attribute_key => $product_attribute ) : ?>
            <tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--<?php echo esc_attr( $product_attribute_key ); ?>">
                <th class="woocommerce-product-attributes-item__label"><?php echo wp_kses_post( $product_attribute['label'] ); ?></th>
                <td class="woocommerce-product-attributes-item__value"><?php echo wp_kses_post( $product_attribute['value'] ); ?></td>
            </tr>
        <?php endforeach; ?>
        <tr class="woocommerce-product-attributes-item woocommerce-product-attributes-statharas-editing-php">
                <th class="woocommerce-product-attributes-item__label">Number of pages</th>
                <td class="woocommerce-product-attributes-item__value"><?php the_field('book_number_of_pages'); ?></td>
            </tr>
    </table>

    What I ‘ve done for now (and it’s working) is instead of the_field function I am using the do_shortcode function, but if you are like me, you know that’s not enough; not knowing why it doesn’t work.

    defined( 'ABSPATH' ) || exit;
    
    if ( ! $product_attributes ) {
    	return;
    }
    ?>
    <table class="woocommerce-product-attributes shop_attributes">
    	<?php foreach ( $product_attributes as $product_attribute_key => $product_attribute ) : ?>
    		<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--<?php echo esc_attr( $product_attribute_key ); ?>">
    			<th class="woocommerce-product-attributes-item__label"><?php echo wp_kses_post( $product_attribute['label'] ); ?></th>
    			<td class="woocommerce-product-attributes-item__value"><?php echo wp_kses_post( $product_attribute['value'] ); ?></td>
    		</tr>
    	<?php endforeach; ?>
    	<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-statharas-editing-php">
    			<th class="woocommerce-product-attributes-item__label">Number of pages</th>
    			<td class="woocommerce-product-attributes-item__value"><?php echo do_shortcode( '[acf field="book-number-of-pages"]' ); ?></td>
    		</tr>
    </table>

    Could anyone please give me a hand here ?

  • Filters didn’t work either, so I continued my struggle and luckily did a test changing field name to camel case thanks to a random tooltip in the filter plug-in. After changing the field name to camel case, the filter worked.

    So after that, went back to this code to change the field name to the new one and said to myself let’s try again; and it worked.

    Seems like the issue was related to the dashes 🤒

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.