0

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?

1 Answer 1

1

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");
    }
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Then how do I check from my array to see if the URL contains one of them?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.