user2579058
user2579058

Reputation:

if(in_array()) not working in Wordpress

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:

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?

Upvotes: 0

Views: 3250

Answers (1)

Sam Aleksov
Sam Aleksov

Reputation: 1201

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");
    }
?>

Upvotes: 1

Related Questions