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 }
?>
2
Answers
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!
You can get URI (example:
http://webpage.com/test/en/
)Result as:
/test/en/
. Then, usingexplode()
function:Result as an array of string:
Using
$items[2]
to check and select image