Hello, I’m new to PHP and I’m currently racking my brain over following problem:
I created a repeater field with the sub fields linktext, url and language. I want the output as an inline list with separators if there are values. The ‘if’ is not a problem anymore, but the output of the list with separators is.
Currently I have this code:
<p><?php
$links = get_field('ti_links');
if($links) {
foreach($links as $link)
{
echo '<a href="' . $link['url'] . '">' . $link['linktext'] . '</a>' . '(' . $link['linksprache'] . ')';
}
}
?></p>
I aim for following output:
Links: Link1 | Link2 | Link3 | etc.
Thanks for any input!
Hi @ckaden
To understand what is wrong with your code, it is recommended to debug it line by line to check whether it is returning the expected data. Please take a look at the debugging tutorial on this page: https://www.advancedcustomfields.com/resources/debug/
Hello James,
thanks for your reply.
It’s not about debugging, the code snippet already works. I just don’t get how to achieve the output I mentioned as the aim – it’s more a styling problem with the separators. I googled a lot but for some reason, I don’t get the hang of it. I was hoping for some input from an experienced coder, I’m not even sure if my approach with foreach makes sense here.
Ok, I’m pretty stucked. I’ll explain one more time what I want to achieve.
I want to build a list of links in one line with a separator like this:
“Links: Link1 | Link2 | Link3 | LinkX…”
I have a repeater field called ti_links with three subfields url, linktext and linksprache.
For each link, I want to echo this:
<a href="$url">$linktext</a> ($linksprache)
I’ve built a new code snippet with the check have_rows and a while loop, but I’m to embarrassed to show it here. At least it doesn’t produce errors but simply prints the values, nothing else. What I don’t get is how to put the subfields into an array and implode it for the output with separator.
See the code examples here: https://www.advancedcustomfields.com/resources/get_sub_field/
This is what I was looking for, found the corresponding snippet after long google sessions:
<?php
$linklist = array();
$links = get_field('ti_links');
if($links) {
foreach($links as $link) {
$linklist[] = '<a href="' . $link['url'] . '">' . $link['linktext'] . '</a>' . ' (' . $link['linksprache'] . ')';
}
echo implode(" | ", $linklist);
}
?>
I just didn’t know how to apply the implode, that was all. Still not sure why this magic works, but it works.
The topic ‘Repeater field and link list’ 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.