Display an image depending on the current URL - php

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 }
?>

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

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 }
?>

Related

Display different content depending on post author

I am trying to display a different image on my page depending on who the Wordpress author of the post is.
So far I have tried a few scripts but none of them work. Any help is greatly appreciated. Here is what I am trying to do.
<?php $author = get_the_author(); ?>
<?php
if ( $author('author1') ) {
echo '
<img src="">;
'
} elseif ( $author('author2') ) {
echo '
<img src="">;
'
} else {
// if neither, echo something else
}
?>
The get_the_author() function return the author's display name as a string. So you have to simply compare the result of get_the_author(). There is no array or object as return value.
So I would go with the following solution using a switch instead of if:
<?php $author = get_the_author(); ?>
<?php
switch($author) {
case 'author1':
echo '<img src="">';
break;
case 'auhtor2':
echo '<img src="">';
break;
default:
// if neither, echo something else
}
?>
In case you want to use the if statement you can use the following:
<?php $author = get_the_author(); ?>
<?php
if ($author === 'author1') {
echo '<img src="">';
} elseif ($author === 'author2') {
echo '<img src="">';
} else {
// if neither, echo something else
}
?>

Display html if one of more variables are not empty

I want to echo some html if a variable isn't empy, for this I know I can do the following:
if (!empty($test)) {
?>
<p>Some nice html can go here</p>
<?php
}
else
{
echo "It's empty...";
}
How can I do this for several variables? So if one of the variables are not empty then echo the html? Would this do it?
if (!empty($test || $image || $anothervar)) {
?>
<p>Some nice html can go here</p>
<?php
}
else
{
echo "It's empty...";
}
Just try with:
if (!empty($test) || !empty($image) || !empty($anothervar)) {
// ...
}
You should check every variable:
!empty($test) || !empty($image) || !empty($anothervar)
empty function does not take multiple arguments.
So, you need to user empty separately for each variable.
The final code should be:
if (!empty($test) || !empty($image) || !empty($anothervar)) {
Just check all the three variables.
Also, I advise you to embed your php into your html to have a better readable document, like this:
<?php if (!empty($test) || !empty($image) || !empty($anothervar)): ?>
<p>Some nice html can go here</p>
<?php else: ?>
It's empty...
<?php endif; ?>
Just another solution:
if(empty($test) and empty($image) and empty($anothervar)) {
echo "It's completely empty...";
} else {
?>
<p>Some nice html can go here</p>
<?php
}
Or if you have a lot of variables to check:
$check = array("test","image","anothervar");
$empty = true;
foreach($check as $var) {
if(! empty($$var)) {
$empty = false;
}
}
if($empty) {
echo "It's completely empty...";
} else {
?>
<p>Some nice html can go here</p>
<?php
}

I'm am trying to change a css property using $_GET but am not getting results

First of all this is a wordpress page so [insert_php] is a replacement for <?php.
The url of the page is as follows "http://**.com/?page_id=98#highlights?sel=q1s"
the code on the page is as follows
<div id="Question1" style="background-color:[insert_php] if ($_GET['sel'] == "q1s") { return "black"; } else { return "white"; };">
I'm sure I'm overlooking something here.
EDIT :
<div id="Question1" style="background-color:[insert_php] if ($_GET['sel'] == "q1s") { echo "black"; } else { echo "white"; }[/insert_php];">
this is my current code and it still showing no results, I checked the actual value I'm getting for background-color and for some reason am getting the result "white".
I'm not sure but I think the parenthesis is missing around the condition, and also you have to use an echo; like this :
if ($_GET['sel'] == "q1s") { echo "black"; }
<div id="Question1" style="background-color:<? if ($_GET['sel'] == "q1s") { echo "black"; } else { echo "white"; };">
you want echo instead of return
You forgot the closing php tag. Normally ?> but since you are using wordpress use [/insert_php]
Also, you should echo instead of return. You are not in a function.
<div id="Question1" style="background-color:[insert_php] if ($_GET['sel'] == "q1s") { echo "black"; } else { echo "white"; }; [/insert_php]">

Tumblr V2 API PHP wrapper

I am trying to integrate a Tumblr blog into my website, using the TumblrPHP wrapper available at https://github.com/gregavola/tumblrPHP. This is code I'm using to view the posts on my site:
<?php
include ('lib/tumblrPHP.php');
$consumer = 'key';
$secret = 'key';
$tumblr = new Tumblr($consumer, $secret);
$posts = $tumblr->get('/blog/nyhetergaius.tumblr.com/posts');
foreach($posts->response->posts as $posts) {
?>
<h2><?php echo date('Y-m-d', $post->timestamp) ?></h2>
<?php if ($post['type'] == 'regular') { ?>
<?php echo $post{'body'}; ?>
<?php } ?>
<?php
if ($post->type == 'photo') {
foreach ($post->photos as $photo) {
?>
<img src="<?php echo $photo->alt_sizes[1]->url ?>" />
<?php
}
echo $post->caption;
}
else
echo $post->body;
?>
<?php
}
?>
I am using my own key and secret key, but even so I can only retrieve a default date for each post. What am I doing wrong? Is there an easier way to view the posts as they appear on the blog http://nyhetergaius.tumblr.com/? This is how the posts appear on my site: http://test.gaius.nu/om.php.
Thank you for your support.
I was able to make this work by replacing $post['type'] with $post->type etc..
foreach($posts->response->posts as $posts) {
$postDate = date('Y-m-d', $posts->timestamp);
echo "<h2>$postDate</h2>";
if ($posts->type == 'regular') {
echo $posts->body;
}
elseif ($posts->type == 'photo') {
foreach ($posts->photos as $photo) {
$imgURL = $photo->alt_sizes[1]->url;
echo "<img src=\"$imgURL\" />";
}
echo $posts->caption;
}
else
{
echo $posts->body;
}
}

Best way to highlight tabs according to the page you're on with CakePHP?

So prior to being introduced to CakePHP, I'd highlight the appropriate navigation tab according to the url with the following (rather sloppy) code I wrote (fyi absolute_url was a function I wrote to get the absolute path) :
$page = $_SERVER['PHP_SELF'];
// add all possible states for the navigation to an array
$checkNav = array(
"index" => "index",
"new" => "new",
"random" => "random",
"submit" => "submit"
);
$compareAgainst = strpos($page, $checkNav['index']);
if ($compareAgainst == 0) {
echo "<li><span class=\"navBorder\">Popular</span></li>\n";
} else {
echo "<li>Popular</li>\n";
}
$compareAgainst = strpos($page, $checkNav['new']);
if ($compareAgainst == 0) {
echo "<li><span class=\"navBorder\">New</span></li>\n";
} else {
echo "<li>New</li>\n";
}
$compareAgainst = strpos($page, $checkNav['random']);
if ($compareAgainst == 0) {
echo "<li><span class=\"navBorder\">Random</span></li>\n";
} else {
echo "<li>Random</li>\n";
}
$compareAgainst = strpos($page, $checkNav['submit']);
if ($compareAgainst == 0) {
echo "<li><span class=\"navBorder\">+ Submit a Link</span></li>\n";
} else {
echo "<li>+ Submit a Link</li>\n";
}
Now, I've noticed that in Cake, to determine the relative path, I can just go:
<?= $this->here; ?>
Is there a better way to do this, or should I just implement this (new) method with the old code?
You can do the following
Add this to app_helper.php if you need it in multiple pages. You feed this function with the controller and the action you want to check you want to compare against. The function compares it with the current page and return true if they match.
function isActive($controller, $actions = array())
{
foreach ($actions as $action)
{
if ($controller == $this->params['controller'] && $action == $this->params['action'])
{
return true;
}
}
return false;
}
And then generate your links like so:
<ul class="left">
<li <?php if($html->isActive('controller_name', array('index'))) { echo 'class="active"'; } ?>><?php echo $html->link('Index', '/index'); ?></li>
<li <?php if($html->isActive('controller_name', array('new'))) { echo 'class="active"'; } ?>><?php echo $html->link('New', '/new'); ?></li>
<li <?php if($html->isActive('controller_name', array('random'))) { echo 'class="active"'; } ?>><?php echo $html->link('Random', '/random'); ?></li>
<li <?php if($html->isActive('controller_name', array('submit'))) { echo 'class="active"'; } ?>><?php echo $html->link('Submit', '/submit'); ?></li>
</ul>
If the function returns true, the link will have class="active". Adapt it to your needs.
The way I've always done this is to give your body tag an id, and use css to target it. If your views are all separate then you can hard code the body id. If you are using some sort of template that adds in the header, content, footer etc., then just pass the id as a variable to the header view or wherever the body tag is (really any outer container/div that will be on every view and contain your navigation tabs). Also you will need to give your navigation tab id's to target each one.
Then just some css like this:
#homepage a#hometab,
#aboutpage a#abouttab,
#productpage a#productstab,
#contactpage a#contacttab
{
special active styling here
}

Categories