1

In wordpress i have an custom field where you can select multiple languages. Now I want to make an if statement so I can display flags for the selected languages.

For example the following languages are selected: NL, EN, DE, IT.

<?php if (in_array('nl', $languagevalue)) { ?>
    NL image
<?php } ?>

This works but I want to display a flags for each selected language. So if the array contains NL show NL-flag and if it also contains EN show EN-flag etc.

How can I achieve such thing?

Regards,

11
  • How does $languagevalue get created? Commented May 22, 2018 at 15:44
  • Assuming it can only be a single value, why not just use else if? Commented May 22, 2018 at 15:45
  • @ExplosionPills with this: $languagevalue = get_field('language'); Commented May 22, 2018 at 15:45
  • 2
    foreach the array and print an image for each of its values. Commented May 22, 2018 at 15:49
  • 1
    Name the images the same as the language codes and simply use the array values for the image names. php.net/manual/it/control-structures.foreach.php Commented May 22, 2018 at 15:52

4 Answers 4

2

Create one image for each language on a folder containing the language code, like so

images/en-flag.png
images/nl-flag.png

And loop thru the array to display the images

foreach ($languagevalue as $lang) {
    echo '<img src="images/' . $lang . '-flag.png" alt=""/>';
}
Sign up to request clarification or add additional context in comments.

2 Comments

I choose this option, because this makes it more dynamically for our client. I can add flags for each language that exists and the customer can create which language they want to use.
@n00bly Just to let you know. There is nothing that is more dynamic with this solution than any of the other looping or my answer. The In_array solutution is hardcoded the other are as dynamic as this. The only difference is if you how you want to loop or if you want to save time and not loop and just use implode.
1

Try something like this , to check existence of each item

 <?php 
 if (in_array('nl', $languagevalue)) { 
     //NL image
 }
 if (in_array('en', $languagevalue)) { 
     //en image
 }
 if (in_array('de', $languagevalue)) { 
     //de image
 }
 if (in_array('it', $languagevalue)) { 
     //it image
 }
 ?>

Comments

1

I would actually create another array with the flag images by iterating over the $languagevalue array:

$flags = [];
foreach($languagevalue as $lang) {
  $flags[] = "$lang-flag";
}

Now you can iterate over $flags.

This assumes you can derive the flag name directly from the language name. If that's not the case, you'll have to use something like a switch statement to map the language name to the flag image name.

Comments

1

You can implode the values with the html of the end and begining of the IMG tag.
This way you don't need to loop or check if the value is in the array. The html is generated automatically.

$languagevalue = ['NL', 'EN', 'DE'];

echo '<img src="images/' . implode('-flag.png" alt=""/> <img src="images/' ,$languagevalue) . '-flag.png" alt=""/>';

https://3v4l.org/D2IMg

Output:

<img src="images/NL-flag.png" alt=""/> 
<img src="images/EN-flag.png" alt=""/> 
<img src="images/DE-flag.png" alt=""/>

Added new lines for clarifying

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.