Support

Account

Home Forums Front-end Issues Option to Trim Checkbox Fields

Solving

Option to Trim Checkbox Fields

    • xaibi

    • March 18, 2019 at 11:51 pm

    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….

    • xaibi

    • March 22, 2019 at 4:28 pm

    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.

    • xaibi

    • March 24, 2019 at 6:53 pm

    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++;
      }
    }
    
    • xaibi

    • March 28, 2019 at 1:31 am

    Should I copy paste that code and change the field only?

    • xaibi

    • January 21, 2020 at 8:37 pm

    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);
    }
    
    • xaibi

    • January 22, 2020 at 1:43 am

    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.

    • xaibi

    • January 22, 2020 at 6:23 pm

    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;
      }
    }
    
    • xaibi

    • June 2, 2020 at 1:06 am
    This reply has been marked as private.
    • xaibi

    • June 12, 2020 at 10:01 pm

    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 
        }
      }
    ?>
    
    • xaibi

    • June 17, 2020 at 7:55 am

    Hello,
    Thank you for your help but this is not working, it’s showing me all checkboxes.

    Regards

    • xaibi

    • June 26, 2020 at 11:15 pm

    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?

    • xaibi

    • June 27, 2020 at 8:35 pm

    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.

    • xaibi

    • June 30, 2020 at 10:14 pm

    @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.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.