Display ACF Label instead of ACF Values

Hi,

Earlier below code was working well but today we saw displaying ACF Values instead of ACF Labels at our website. We have used below codes to display ACF choices.

//Products Hair Type
$hairs = get_field('products_hair_type');
$num_of_items_hair = count($hairs);
$num_count_hair = 0;
if( $hairs ): ?>
<strong>Hair Type:</strong>
<?php foreach( $hairs as $hair ): 
    echo $hair; 
    $num_count_hair = $num_count_hair + 1;
    if ($num_count_hair < $num_of_items_hair) {
      echo ", ";
  }
  endforeach; 
?>  <br />
<?php endif;

At Choices products_hair_type we have as below:

all_hair_types : All Hair Types
dry_hair : Dry Hair
dull_hair : Dull Hair
fizzy_hair : Fizzy Hair
flat_hair : Flat Hair
normal_hair : Normal Hair
oily_hair : Oily Hair
thin_hair : Thin Hair

We need to display All Hair Types instead of all_hair_types and more selected choices if another too. Can you please suggest solutions in this matter.

Example Link:
https://www.hamroshringar.com/product/bare-anatomy-volumizing-hair-shampoo-250-ml/

Hoping for the solution.

Thank You,
Bibhash Karn

Hello Bibhash,

Thanks for writing in!

Be advised that custom PHP coding is beyond the scope of our support under our Support Policy. If you are unfamiliar with code and resolving potential conflicts, you may select our One service for further assistance.

In the meantime, before running the for...each() loop, add var_dump($hairs); so that you can check what are the array value of the $hairs variable. And play around with the return value of your field from value, label or both (array).

Kindly let us know how it goes.

Hi,

Thank you for your response.

As per your suggestion, we used var_dump($hairs); and had below output:

array(2) { [0]=> string(9) "flat_hair" [1]=> string(9) "thin_hair" } 

As per your above screenshot we have been using Label as the Return Value for our custom field. Also, changing the Return Value , the output of var_dump($hairs); was same in the Value and Both (Array) as it was for Label

Hoping for the solution.

Thank You

Hey Bibhash,

You’re using get_field() in your code and that really gets the value according to ACF documentation here: https://www.advancedcustomfields.com/resources/get_field/

You might want to use get_field_object() instead. Check out the usage here: https://www.advancedcustomfields.com/resources/get_field_object/

That is all we can help you with. Custom coding is beyond the scope of our theme support.

Hope that helps and thank you for understanding.

Hi,

Thank You for your response.

We have already tried the get_field_object() as you mentioned. But get_field_object() is returning blank (No Value as well as no Label).

Though, we will try to explore and go through further and will let you know if any further information is required.

Thank You

You are most welcome Bibhash

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.