Home › Forums › General Issues › Link field values (checkbox) with url-s?
It seems to be a simple thing, but did not find an answer yet – how is it possible to add URL links to custom fields?
Let’s say I have a field for different organizations, with a name ‘field_organization’ and field type is checkbox. The user assigns the organizations (predefined choices by the checkbox), usually one, but sometimes several, to posts.
So how can I assign to every one of theses organizations a specific URL (which points to their websites), so I could later make the connection in frontend?
You will need to have this information stored in another location, and that location would depend on how the information relates to other content. For example you might have a custom post type called “Organization” and then you can enter all the information about the organization there then use a relationship field to allow selection and getting the needed information in the front end.
There are probably other possibilities, but like I said, it would depend on what you’re doing.
Thanks for the swift reply. I tried this solution, but as far as I managed to get with the relationship, it presumes that I have a post for every organization, where the URL is specified?
Isn’t there any other and simpler solution, e.g having checkboxed field for URLs, where the names of the organizations are just replaced with URLs (the order of values is the same).
So for example if the chosen value in organization_field is for example third in this checkbox list, then I will just take the third value from url_field and link them together in frontend?
A check box field returns an array of selected values
you could make the checkbox choices something like
http://site1.com/ : Organization 1
http://site2.com/ : Organization 2
http://site3.com/ : Organization 2
then in you’re template you can do something like this
$field_value = get_gield('checkbox_field'); // returns array of selected values
$field_object = get_field_object('checkbox_field'); // gets field settings
$choices = $field_object['choices'];
foreach ($choices as $value => $label) {
if (in_array($value, $field_value) {
?>
<a href="<?php echo $value; ?>"><?php echo $label; ?></a>
<?php
}
}
Thanks, it works perfectly!
Only one more question: if I have multiple selections in checkboxes, how can I use the implode correctly? At the moment I tried this:
$myArray = array();
foreach($choices as $value => $label) {
if (in_array($value, $field_value)) {
$myArray[] = "<a href='$value'>$label</a>";
echo implode( ', ', $myArray);
} }
But it kind messes something up.
do the implode after the loop
$myArray = array();
foreach($choices as $value => $label) {
if (in_array($value, $field_value)) {
$myArray[] = "<a href='$value'>$label</a>";
} }
echo implode( ', ', $myArray);
Hi, I am trying to achieve a similar thing, using a checkbox to display name with a url in a list separated with hr line, so it looks nicer. The problem I am having is that I keep getting the first variable displayed in each list point. What am I missing? Thank you for your help.
This is the result I am getting (url is being linked from the word website, but not here):
Website1
Website1
Website1
Instead of:
Website1
Website2
Website3
<ul class="service-list">
<hr class="white-sep" />
<?php $count=0;
$globalCount=0;
$field_value = get_field('services_test2'); // returns array of selected values
$field_object = get_field_object('services_test2'); // gets field settings
$services = $field_object['choices'];
foreach($services as $value => $label){
$count++;
$globalCount++;
$class='';
$hr ='';
$hrstart ='';
if (in_array($value, $field_value)) {
$servicesList = "<a href='$value'>$label</a>";
}
if($count==2){
//$class='odd';
$hr='<hr class="black-sep" />';
} elseif($count==4){
//$class='even';
$hr='<hr class="white-sep" />';
}
if($count==4){
$count=0;
}
?>
<li class=""><?php echo str_replace('&', '<span class="pink">&</span>', $servicesList); ?></li>
<?php echo $hr; ?>
<?php } ?>
</ul>
I try to remove some lines and done this but got 2 problems.
1st is no commas and space in between text
2nd is the last field comes 2 times
Screenshot: https://prnt.sc/low0fl
<?php
$field_value = get_field('job_qualification');
$field_object = get_field_object('job_qualification');
$services = $field_object['choices'];
foreach($services as $value => $label){
if (in_array($value, $field_value)) {
$servicesList = "<a target='_blank' href='https://jobsalert.pk/edu/$value'>$label</a>";
}?>
<?php echo str_replace(',', ' , ', $servicesList); ?>
<?php } ?>
The topic ‘Link field values (checkbox) with url-s?’ 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.