-2

I have my template, and I want it to display a certain image if you are on certain page like http://example.com/test and if you aren't on that page, then I want it to display another image.

I also want it to display the image if you are in any sub directory like http://example.com/test/stuff

Also, is there a way to do this with multiple pages in the same code?

So like

if page = example.com/test then display testimg.jpg

if page = example.com/archive then display archive.jpg

else, display defaultimg.jpg

thanks!

3

2 Answers 2

0
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if ( strpos($url, 'test') !== false ) {
    echo('<img src="image_path/testimg.jpg">');
}
elseif ( strpos($url, 'archive') !== false ) {
    echo('<img src="image_path/archive.jpg">');
}
else {
    echo('<img src="image_path/defaultimg.jpg">');
}
Sign up to request clarification or add additional context in comments.

1 Comment

Function strpos() may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function. php.net/manual/en/function.strpos.php
0

U can also using strpbrk() function and get more compact code: (>PHP5)

$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if ( strpbrk($url, 'test') ) {
    echo('<img src="image_path/testimg.jpg">');
}
elseif ( strpbrk($url, 'archive') ) {
    echo('<img src="image_path/archive.jpg">');
}
else {
    echo('<img src="image_path/defaultimg.jpg">');
}

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.