Home › Forums › Front-end Issues › How do i display custom fields using Function.php
Hi I’ve got ACF working and displaying my custom fields inside a WooCommerce product page template using the following code:
<?php
$fields = get_field_objects();
if( $fields ): ?>
<ul>
<?php foreach( $fields as $field ): ?>
<li><?php echo $field['label']; ?>: <?php echo $field['value']; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
However, I want to display the fields and control how to position them on the front-end using the functions.php file. At the moment, the fields are showing above the product title, but need to be appearing below it. From what I can see, I need to get this done using the functions.php file.
Can anyone help with this?
I think what you need to do is find the hook where you want these things displayed, have a look here:
https://businessbloomer.com/woocommerce-visual-hook-guide-single-product-page/
I think the closest hook is woocommerce_before_add_to_cart_form.
Try this in your functions.php file:
add_action('woocommerce_before_add_to_cart_form', 'display_product_fields' );
function display_product_fields() {
$fields = get_field_objects();
if( $fields ): ?>
<ul>
<?php foreach( $fields as $field ): ?>
<li><?php echo $field['label']; ?>: <?php echo $field['value']; ?></li>
<?php endforeach; ?>
</ul>
<?php endif;
}?>
That worked perfectly. Thanks for your help. That link to woocommerce hooks is really helpful too.
Hi,
I have added this code to a dev site I’m working on is the code meant to show all the custom fields added to a particular product as it’s only showing the first one for me does anyone know why this is happening?
Thanks,
Jim
Works perfect! One more question, if i have 3 custom fields. How i can display them separately?
One hook for one field, Another for another.
Can you help?
The topic ‘How do i display custom fields using Function.php’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.