0

I am setting up a website and want to display a different image depending on the current page URL

If http://webpage.com/test/en/ then display image 1
If http://webpage.com/test/fr/ then display image 2
If http://webpage.com/test/nl/ then display image 3

New to this but managed to do something similar but there were only 2 options! any help appreciated:

<?php if ($autoshowroom_video) { ?>
<iframe src="<?php echo esc_url($autoshowroom_video); ?>
?autoplay=1&loop=1&modestbranding=1&showinfo=0&rel=0&iv_load_policy=3&controls=0" width="310" height="205" frameborder="0"></iframe>
<?php } else { ?>
<video width="100%" height="auto" autoplay loop muted>
<source src="https://test.co.uk/Promo.mp4" type="video/mp4" />
<?php } ?>

I have been searching the net and found this, which although dosnt do exactly what I want would at least help me understand a little more as I am trying to learn.

<?php $host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if($host == 'webpage.com/test/fr/') 
{
    echo $this->__('French');
}
else
{
    echo $this->__('Another Language');
} ?>

I finally made some progress!

I need to do this check several times, I have 5 languages on the site. Can I run several 'ifs'? or will it cancel out after the first or does it wait for a result?

<?php $language = (get_locale()) ?>
<?php if($language == 'en_GB') { ?>
<video width="100%" height="auto" autoplay loop muted>
<source src="https://test.co.uk/Promo.mp4" />
<?php } else { ?>
Not English so no video
<?php } ?>

So... as a noob I am proud of myself! I am sure there is a cleaner way to do this, but the below does what I want! Any feedback appreciated!

<?php $language = (get_locale()) ?>
<?php if($language == 'en_GB') { ?>English<?php }
if($language == 'pl_PL') { ?>Polish<?php }
if($language == 'es_ES') { ?>Spanish<?php }
if($language == 'nl_NL') { ?>Dutch<?php }
if($language == 'tl') { ?>Tagalog<?php }
if($language == 'pl_PL2') { ?>Polish 2<?php }
if($language == 'pt_PT') { ?>Portuguese<?php }
?>
PaulTDS
  • 9
  • 1
  • Does this answer your question? [how to capture the current page url in PHP](https://stackoverflow.com/questions/10816378/how-to-capture-the-current-page-url-in-php) – Kinglish Jun 12 '21 at 15:40
  • Are you using wpml plugin? – Akshay Shah Jun 12 '21 at 16:50
  • What's the exact problem with the given code? What's wrong in using several `if` conditions? – Nico Haase Jun 13 '21 at 10:10
  • Yes! LoL What's wrong with using several if conditions! That's what I did in the end I didn't bother with an else as there never will be? Thanks for all of your inputs! – PaulTDS Jun 13 '21 at 15:18

2 Answers2

0

You can get URI (example: http://webpage.com/test/en/)

$uri = $_SERVER['REQUEST_URI'];

Result as: /test/en/. Then, using explode() function:

$items = explode("/", $uri);

Result as an array of string:

["", "test", "en", ""]

Using $items[2]to check and select image

mac thiem
  • 86
  • 2
0

So... as a noob I am proud of myself! I am sure there is a cleaner way to do this, but the below does what I want! Any feedback appreciated!

<?php $language = (get_locale()) ?>
<?php if($language == 'en_GB') { ?>English<?php }
if($language == 'pl_PL') { ?>Polish<?php }
if($language == 'es_ES') { ?>Spanish<?php }
if($language == 'nl_NL') { ?>Dutch<?php }
if($language == 'tl') { ?>Tagalog<?php }
if($language == 'pl_PL2') { ?>Polish 2<?php }
if($language == 'pt_PT') { ?>Portuguese<?php }
?>
PaulTDS
  • 9
  • 1