Home › Forums › Add-ons › Repeater Field › Repeaters – Get Sub-Field Siblings
Hi 🙂
So, I’m trying to use ACF Repeater fields to make a rudimentary translation system but I’m failing on something that feels like it should be easy. I’ve searched but to no avail.
Each Page already has English content and each Page can have multiple translations, which may or may not be different per page.
I have created a repeater field (called ‘page_translations’). This repeater field has 3 sub-fields:
1. A select dropdown where a user can choose a language to translate to (called ‘choose_language’). The value of each <option>
field is a two letter country code (e.g ‘fr’ for french)
2. A text field to enter the Page title in the new language (called ‘translated_title’)
3. A WYSIWYG editor field to enter the Page content in the new language (called ‘translated_content’)
So, if an author wanted to add a French translation of a Page, they’d click the repeater button under the English content, specify French as the language then add the title and content in French. If they wanted another language too, they’d click the repeater button again and repeat the process for the new language.
I intend to re-load each Page that has translations via a clickable URL with &tr=fr
(‘fr’ is french in this example) appended to each link.
What I was planning to do was detect if tr
is set and has a value. If not, display English but if it has then pull out the translated title and content.
I assumed ACF Repeater functions would include a ‘sibling’ call e.g. if tr equals fr get the two siblings associated with that repeater (title and content). But it seems I was wrong.
Is there something obvious I’m missing here? If no, is there a way I can get to the siblings of the chosen language?
I have found a way to bodge this using CSS. It’s not pretty and it does mean looping through all the translations so if anyone knows a better way I’d be very happy to hear it. Here’s what I’m doing:
function tsltContent() {
if(!isset($_GET['tr']) || empty($_GET['tr'])) {
$finalStr = "<h2>" . get_the_title() . "</h2>";
$getallcontent = apply_filters('the_content', get_the_content());
$finalStr .= $getallcontent;
}else{
$trlang = $_GET['tr'];
$finalStr;
if(have_rows('page_translations') ):
while(have_rows('page_translations')): the_row();
$newlang = get_sub_field('choose_language');
if($trlang == $newlang) {
$markup = '<div class="show-lang">';
}else{
$markup = '<div class="hide-lang">';
}
if(have_rows('translation_elements')):
while(have_rows('translation_elements')): the_row();
$trStrTitle = get_sub_field('translated_title');
$trStrContent = get_sub_field('translated_content');
$finalStr .= $markup . "<h2>" . $trStrTitle . "</h2>" . $trStrContent . "</div>";
endwhile;
endif;
endwhile;
endif;
}
return $finalStr;
}
You must be logged in to reply to this topic.
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.