Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using Wordpress latest version and I this piece of code inside my header.php:

<?php
    $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $slugMenu = array (
        'planes',
        'two-wings',
        'four-wings'
    );
    if (in_array($url, $slugMenu)) {
     echo "
        <style>
            .planes,
            .two-wings,
            .four-wings { background:#101010; }
        </style>
        ";
    }
    else {
        echo _("not found");
    }
?>

The code output should be like this:

  • Check to see if the URL contains one of the elements in declared array
  • If it does, then echo inside the properties and values

Well, for some reason, just doesn't work. All I get is else " not found" instead of actually having the <style> inside the <head>.

The thing is that it was working on another project of mine based on same Wordpress. Am I doing something wrong here?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You are checking if one of the array elements contains the whole request URI and none of your elements does.

Details:

This returns true:

if (in_array("planes", $slugMenu))

This returns false:

if (in_array("http://planes.com/planes", $slugMenu))

The solution depends on a variety of factors but one would be:

<?php
    $uri = $_SERVER[REQUEST_URI];
    $slugMenu = array (
        '/planes',
        '/two-wings',
        '/four-wings'
    );
    if(in_array($uri, $slugMenu)) 
    {
        echo "
        <style>
            .planes,
            .two-wings,
            .four-wings { background:#101010; }
        </style>
        ";
    }
    else 
    {
        echo _("not found");
    }
?>
share|improve this answer
    
Then how do I check from my array to see if the URL contains one of them? –  sg1asgard Jul 13 '13 at 11:28
    
You can use PHP strstr() -> php.net/manual/en/function.strstr.php –  Steven Jul 13 '13 at 11:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.