Support

Account

Home Forums Front-end Issues Option to Trim Checkbox Fields

Solving

Option to Trim Checkbox Fields

  • How I can Trim the the checkbox fields, let say I checked 4 options and want to display 2 options and other options would be display in tool tip….

  • Any help please…

  • Are you talking about in the front end somehow? In the admin? What code are you using for the tool tips? There really isn’t enough information here to be able to ask clarifying questions. If you give more detail someone may be able to help you.

  • Yes I am talking about front end. I am using this code..

    <p>Colors: <?php the_field('colors'); ?></p>

    This is checkbox, let say I have checked 6 checkboxes and its showing me 6 options in front end..

    e.g in front end..

    USA, UAE, Canada, Pakistan, England, China,

    But I want to trim them to show only 2 options not to trim the words.

    e.g
    USA, UAE…

    Not this;
    USA, UAE, Canada, Pakistan, Eng…

    And currently did not using any tool tip code.

  • You cannot do this using the_field().

    
    $values = get_field('colors');
    if ($values) {
      $count = 0;
      foreach ($values as $value) {
        if ($count < 2) {
          // do one thing with the first 2
        } else {
          // do something else with the rest
        }
        $count++;
      }
    }
    
  • Should I copy paste that code and change the field only?

  • How I can do trim with this?

    $values = get_field('job_location');
    if( count($values)){
        foreach($values as $k=>$value){
            if($k) echo ', ';
            echo $value;
        }
    }
  • 
    $values = get_field('job_location');
    if ($values) {
      echo implode(', ', $values);
    }
    
  • john this is for comma implode, but how I can trim the checkboxes with comma implode.

  • Checkbox values should not need to be trimmed. ACF removes spaces around values and labels automatically.

  • Actually I want to trim or limit the content, as I said that I want to show some of the checkboxes not all.

  • sorry, I missed that part, you need to set up a counter and break out of the loop when that number is reached

    
    $counter = 0;
    foreach ($checkbox as $value) {
      // output value
      $counter++;
      if (counter == 2) {
        echo ', ...';
        break;
      }
    }
    
  • This reply has been marked as private.
  • I really appreciate your help, I waiting for your response.

  • 
    <?php 
      $checkbox_values = get_field('job_location', $post->post_parent); 
      if ($checkbox_values) {
        $count = 0;
        foreach ($checkbox_values as $value) {
          if ($count == 2) {
            break;
          }
          $string = "$value";
          $string = str_replace(" ", "-", $string);
          $string = strtolower($string);
          ?>
            <a target="_blank" href="https://jobsalert.pk/city/<?php echo $string; ?>"><?php echo $value; ?></a>,
          <?php 
        }
      }
    ?>
    
  • Hello,
    Thank you for your help but this is not working, it’s showing me all checkboxes.

    Regards

  • Hello, @john I test it and it’s not working, help me, please.

  • Without more information I don’t know what else I can do for you. What is it doing? What is it not doing? What do you expect it to do that it’s not doing?

  • Want to Trim the checkbox fields, let say I checked 4 options and want to display 2 options and other options would be displayed in the tool tip.

    I use your code which you gave me and it’s not trimming the checkboxes.

  • @john Huebner, did you got it?

  • The code I supplied should be doing what you want. Without information on what it’s doing that’s unexpected I don’t now why it would not be working.

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

You must be logged in to reply to this topic.