Home › Forums › Front-end Issues › 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….
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++;
}
}
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;
}
}
<?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
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.
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.
The topic ‘Option to Trim Checkbox Fields’ 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.