Home › Forums › General Issues › Use selected checkbox values in a URL
Hi there,
I’m probably going about this all the wrong way, so I’m looking for a bit of assistance!
Essentially, I’ve got a list of areas that people work in set up as a checkbox list. I’ve also got separate pages created for each area.
What I’m trying to do is loop round the array to pull out the values of each selected area and append them to a partial URL to complete it to match the page URL.
I’ve gone round and round, but so far have got this!:
<?php
$values = get_field('regions-listed-in');
if($values)
{
foreach($values as $value)
{
<a href="http://test/inhouse/$value">$value</a>
}
}
?>
Any help will be much appreciated.
Cheers,
Lerq
Hi @lerq
Can you give some examples of expected results for both 1 value checked and 3 values checked?
Will there ever be multiple values checked? Does each checked value create a new link? Or does it add to the 1 link url?
Perhaps all you are missing is the correct PHP syntax:
echo ' <a href="http://test/inhouse/' . $value . '">$value</a>';
Hi Elliot,
Thanks for the response.
Yup, there will often be multiple values selected, and I’d like them to point to different URLs.
For example, if there are 5 regions available in the checkbox list (a – e), and Joe works in 3 of them (a, b and d) I’d like the output to be:
“Joe is available in: a b d”
with the a b and d text linking to the respective area pages (http://test/inhouse/a, http://test/inhouse/b and http://test/inhouse/d)
If Jane only works in area c, then the output would be:
“Jane is available in: c”
with the c linking to http://test/inhouse/c
Cheers,
Lerq
Hi @lerq
Thanks for the info.
Yep, your code should look like your origional loop, but use my ‘echo’ code within the loop to render the link.
Easy
Thanks
E
Hi Elliot,
Brilliant! Thank you for your help on this, works a treat!
Although, I had to wrap the second $value (that displays the text for the link) in the single quotes and full stops to get it to render the text correctly:
<?php
$values = get_field('regions-listed-in');
if($values)
{
foreach($values as $value)
{
echo ' <a href="http://test/inhouse1/explore/' . $value . '">' . $value . '</a>';
}
}
?>
Hi,
I am trying the same things but this is not working for me. I would like to display the field label hyperlinked to the value. i.e. Display ‘Red’ and when you click on ‘Red’ got http://www.mywebsite.com/red.
How can I adjust this code to accomplish that?
Thank you
The topic ‘Use selected checkbox values in a URL’ 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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.