如何在PHP函数中添加另一个页面ID - 新手问?

时间:2013-03-19 18:30:56

标签: php

我需要一些帮助,看看我哪里出错了。

我正在尝试为此原始函数添加页面ID:

<?php if( $post->ID != '91' )
    {
        get_sidebar();
    } ?>
>

也要排除ID 1267。 我正在尝试这个,没有成功。

<?php
    $pageIDs_to_exclude=array("91","1267");

    if( $post->ID != $pageIDs_to_exclude )
    {
        get_sidebar();
    }
?>

当然必须有更好的方法吗?或者我错过了什么? 为任何帮助提供帮助 /安德斯

4 个答案:

答案 0 :(得分:5)

$pageIDs_to_exclude = array("91","1267");

// in_array will return false if it doesn't find $post->ID within the $pageIDs_to_exclude array 
if( ! in_array($post->ID, $pageIDS_to_exclude) )
{
    get_sidebar();
}

答案 1 :(得分:3)

您正试图将$post->ID$pageIDs_to_exclude直接比较,即数组。由于$post->ID不是数组(它是一个字符串),这是不可能的。相反,请查看$post->ID中是否有$pageIDs_to_exclude

if (!in_array($post->ID, $pageIDs_to_exclude)) {

    get_sidebar();

}

in_array()是一个函数,如果在数组中找到该对象,它将返回true

答案 2 :(得分:1)

你可以使用php的in_array。它将返回true或false。

$pageIDs_to_exclude=array("91","1267");

if(!in_array($post->ID,$pageIDs_to_exclude))
{
    get_sidebar();
}

答案 3 :(得分:1)

使用PHP函数in_array()http://php.net/manual/en/function.in-array.php)搜索数组中的值:

<?php
  $page_ids = array("91", "1271");
  if(!in_array($post->ID, $page_ids))
   {
    get_sidebar();
   }
?>