Support

Account

Home Forums General Issues Check Box Output Commas

Solving

Check Box Output Commas

  • Hi, I have read many answers about using “implode” to solve the issue about arrays and commas. Those where applied on a template file or code. But my case is that I am calling the value via [acf field="myfield"], which is inserted on a WordPress Post.

    I was able to create and display the value, however, the outcome shows a comma right after each value. How can I remove it, or is there any other way to do it other than CheckBox? Here is an example of my output:

    This is sentence one.,
    This is sentence two.,
    This is sentence three.

    How can I remove those commas?

    Thanks in advanced.
    Russ

  • The only way to do this would be to code an acf/format_value filter.

    
    add_filter('acf/format_value/name=myfield', 'my_format_checkbox_field_function', 20, 3);
    function my_format_checkbox_field_function($value, $post_id, $field) {
      $value = $field['value'];
      $values = array();
      foreach ($value as $item) {
        if (isset($field['choices'][$item])) {
          $values[] = $field['choices'][ $item ];
        } else {
          $values[] = $item;
        }
      }
      $value = implode('<br>', $values);
      return $value;
    }
    
  • Hi Jonh,

    Thanks for the reply; I appreciate it.

    As I understand it, I included the code in the functions.php of the WP theme. Then, I replaced the “myfield” with the actual one, which is “rating_index” – from [acf field="rating_index"] shortcode.

    Doing so, no value is shown on the front-end. I may be doing it wrong. I am sorry; I am not a coder and rely only on WP and Plugins to build a website. Please understand.

    Russ

  • Yes, that is correct. Either you have not value in the post or ACF does not know what post to get the values from.

    Where is the value added? Are you trying to get the value from the current post being displayed? Need more information on the field setup and how you are trying to use it.

  • In the meantime, I am using the example value and Label like red : Red, blue : Blue, yellow : Yellow just to make sure that I don’t have any duplicate values or labels. Without that filter, the outcome is “red, blue, yellow”. But when I inserted the filter in functions.php, there was no output on the front-end.

    The value is added to the Edit/Post of WordPress. It is the current post (please see the image). And I am planning to use this field on every post on my website.

    ACF Filter

  • The only thing that I can think of is that $field['value'] has already been formatted and is not the expected array. To test this modify the start of the filter to

    
    $value = $field['value'];
    return $value;
    

    and see what happens.

  • Hi John. I don’t know if I did it as instructed. I replaced lines 3 and 4 with:
    $value = $field[‘value’];
    $values = array();

    The result is still the same, no output on the front-end.

    One thing I noticed is that WHATEVER value I put in the “myfield” the front-end will display the array format “red, blue, yellow.” Hence, changing it with the actual value “colors” do not produce an output.

    Thanks for your patience, Sir.

  • I’m trying to see if ACF is formatting the value of $field[‘value’];

    
    add_filter('acf/format_value/name=myfield', 'my_format_checkbox_field_function', 20, 3);
    function my_format_checkbox_field_function($value, $post_id, $field) {
      $value = $field['value'];
      // return the value here so you can see what it is
      // this is temporary, let me know what gets output to the page
      return $value
      
      
      $values = array();
      foreach ($value as $item) {
        if (isset($field['choices'][$item])) {
          $values[] = $field['choices'][ $item ];
        } else {
          $values[] = $item;
        }
      }
      $value = implode('<br>', $values);
      return $value;
    }
    
  • Hi John,

    Pasting it as-is, gives an error:
    syntax error, unexpected variable “$values”, expecting “;”

    So, I put “;” on line 6
    return $value;

    It is still not showing an output when I replace myfield with colors. Leaving the “myfield” as it is displays an output of red, blue, yellow.

    Would it be much easier for you to see what causes this if I will give you access to my WP site?

  • I forgot the semi-colon on the line, should be

    
    return $value;
    
  • Hi, yes John that is what I did. Sadly, the outcome is the same. Leaving the “myfield” untouched shows red, blue, yellow. But when I replaced it with “colors,” the output was blank.

    If I use a different name, like colorsX, it displays red, blue, yellow on the front-end.

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

You must be logged in to reply to this topic.