0

我正在使用 Wordpress 最新版本,并且我在 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");
    }
?>

代码输出应该是这样的:

  • 检查 URL 是否包含声明数组中的元素之一
  • 如果是,则在属性和值内回显

好吧,由于某种原因,它不起作用。我得到的只是“未找到”,而不是实际<style>拥有<head>.

问题是它正在开发基于相同 Wordpress 的我的另一个项目。我在这里做错了吗?

4

1 回答 1

1

您正在检查其中一个数组元素是否包含整个请求 URI,而您的任何元素都不包含。

细节:

这返回真:

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

这返回错误:

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

解决方案取决于多种因素,但其中之一是:

<?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");
    }
?>
于 2013-07-13T11:23:50.293 回答