最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】wordpress中出现 Warning: in_array() expects parameter 2 to be array, null given in

WordPress crifan 8652浏览 0评论

【问题】

wordpress,retina主题中,添加了一些代码后,运行出错,错误信息:

Warning: in_array() expects parameter 2 to be array, null given in E:\Dev_Root\WebServer\wamp\www\wordpress\wp-content\themes\retina\lib\structure\post.php on line 62

Call Stack

#TimeMemoryFunctionLocation
10.0014365952{main}( )..\index.php:0
20.0022369816require( ‘E:\Dev_Root\WebServer\wamp\www\wordpress\wp-blog-header.php’ )..\index.php:17
30.447923341168require_once( ‘E:\Dev_Root\WebServer\wamp\www\wordpress\wp-includes\template-loader.php’ )..\wp-blog-header.php:16
40.450023351432include( ‘E:\Dev_Root\WebServer\wamp\www\wordpress\wp-content\themes\retina\single.php’ )..\template-loader.php:43
50.478823408656get_template_part( )..\single.php:14
60.478823409160locate_template( )..\general-template.php:128
70.479123409288load_template( )..\theme.php:1091
80.479623431224require( ‘E:\Dev_Root\WebServer\wamp\www\wordpress\wp-content\themes\retina\content-single.php’ )..\theme.php:1117
90.486423432104retina_post_copyright( )..\content-single.php:24
100.486523432272in_array ( )..\post.php:62

 

截图如下:

warning in_array() expects parameters 2 to be array, null given in xxx

warning in_array() expects parameters 2 to be array

 

【解决过程】

1.仔细看了错误信息,才明白其说的意思是

对于post.php中的这段代码:

if (!in_array('copyright', get_post_custom_keys())){

中的in_arrar()来说,第二个参数中,其期望的是一个array数组,但是实际上传入的值,此处即get_post_custom_keys(),是null。

所以才报了此错误。

2.然后就去分析为何此处get_post_custom_keys()所返回的值是null。

3.接着去参考之前自己的帖子

【整理】给wordpress的文章添加形如转载请注明,本文链接等版权声明信息

去找到当时是如何添加这个自定义的键值的,然后知道了,是在wordpress后台,新建文章时,勾选右上角的”显示选项“的“自定义栏目”,下面就会出现”自定义栏目“。

不过我此处很悲催的是,连右上角的”显示选项“都不显示了。。。

经过一番折腾后才发现,原来是,在新建页面的顶端显示了一行关于3.4.1的wordpress更新的提示,覆盖遮住了那个”显示选项“了。。

最后先去更新成为3.4.1的wordpress后,更新提示没了,就可以看到”显示选项“了。

4.然后去添加自定义项copyright后,发布测试帖子,结果该测试帖子是没了此错误提示了,但是其他帖子,还是此错误提示。

即,该贴由于有了对应的copyright选项,get_post_custom_keys()得到的值非空,所以in_array可以正常执行,但是其他帖子还是没有custom key导致in_array的第二个参数还是null,所以还是会有此警告。

5.但是很奇怪的是,此处的代码:

/** Copyright Info */
function retina_post_copyright() {
    $output = '';

    if (!in_array('copyright', get_post_custom_keys())){
        $mine_info = sprintf( '
                        <div class="postcopyright">
                        防君子不防也无法防小人的声明:
                        <br />
                        如未注明转载则均为crifan原创,对于原创文章,转载请注明出处:<a href="%1$s" title="%2$s"><strong>%3$s - %4$s</strong></a>
                        <br />
                        本文链接:<a href="%5$s" title="%6$s"><strong>%7$s</strong></a>
                        </div>',
                        get_bloginfo('siteurl'), get_bloginfo('name'), get_bloginfo('name'), get_bloginfo('description'), get_permalink(), the_title_attribute( 'echo=0' ), the_title_attribute( 'echo=0' ));
        $output = $mine_info;
    }
    else {
        $copyright_value = get_post_custom_values('copyright');
        $other_info = sprintf( '
                        <div class="postcopyright">
                        本文由crifan转载自:<a target="_blank" rel="nofollow" href="%1$s" >%2$s</a>
                        <br />
                        本文链接:<a href="%3$s" title="%4$s">%5$s</a>
                        </div>',
                        $copyright_value[0], $copyright_value[0], get_permalink(), the_title_attribute( 'echo=0' ), the_title_attribute( 'echo=0' ));
        $output = $other_info;
    }

    return $output;
}

放在我的在线网站crifan.com中,却是可以正常执行,没有出错的。

为何本地的同样的版本的wordpress和retina主题,却会有此警告呢。

6.然后就去crifan.com后台新建一个帖子,比较一下界面和内容有何区别,结果果然发现不一样:

custom not null

而本地的wordpress中,后台新建文章时,对应的自定义栏目中的选项是空的,所以会显示警告。

7.但是,依稀记得,之前也是只需要按照上面那个操作,只操作一次,这样对于custom key一项,就是写入数据库了,

这样之后再新建帖子的时候,也就是有对应的选项了。

所以,其他帖子再去显示的时候,应该也就可以找到此项了,就改都不会再出现,get_post_custom_keys()还是返回null的问题了。

但是此处去查看帖子时,发现除了那个测试帖,其他的帖子,还是由此警告出现,还是没有消失。

然后又去看了看,再次新建一个帖子,发现自定义一栏,的确就已经有了对应的copyright项了:

local wp custom has copyright

 

但是如上所说, 很奇怪的是,其他帖子的警告还是没有消失,不知道为何。

8.然后又去随便测试了一下,对于一个出现的警告的帖子,再次编辑了下,然后其他设置都不变,只是点击更新,结果更新后的帖子,警告就消失了。

感觉好像是数据库对于其他帖子没有及时更新的缘故?

不太懂。总之,此时,对于新建帖子和更新后的帖子,肯定是没了此警告了。但是对于旧帖子,还是存在此警告的。

根本的原因,仍不确定。

9.尝试切换到默认的Twenty Eleven主题,再切换回retina主题试试。结果问题依旧。

10. 期间还以为是函数get_post_custom_keys()的使用有问题,没有添加postId呢,后来去查找了官网中关于get_post_custom_keys的解释:

Function Reference/get post custom keys

得知本身使用get_post_custom_keys就是可以不加此postId参数的。默认为当前post的id,完全是可以正常使用的。所以此处函数用法没错误。

11.后来实在惹急了,手动去把代码改了,把:

if (!in_array('copyright', get_post_custom_keys())){

改为:

    $cur_post_custom_keys = get_post_custom_keys();
    if (($cur_post_custom_keys==null) || !in_array('copyright', $cur_post_custom_keys)){

这样,就彻底杜绝了上述警告了。

 

【总结】

出现错误:

Warning: in_array() expects parameter 2 to be array, null given in

的原因很简单,即php的函数in_array的第二个参数要求是数组array类型的变量,

而如果传递的参数是空值null,则会报此警告。

至于为何会传入null,则是需要你调试你的代码,分析的你代码,才知道的了。

我此处原因是:

由于wordpress的帖子post的自定义键值custom key为空,所以是null。

所以报错。

而当前本地的wordpress中的post的自定义键值custom key为空,则是由于当前wordpress是比较新的建立的,

很多个帖子的自定义键值,的确为空,所以才会的确是null。

而在线网站上面之所以不是null,则是由于该wordpress网站,是用了比较久了,装了很多插件,所以经过这些折腾,帖子的custom key中,已经有了值了,所以get_post_custom_keys()返回的值就不是空的了,所以就没了上述警告。

因此,把对应代码改了,就可以了,修改后的代码,如下:

/** Copyright Info */
function retina_post_copyright() {
    $output = '';

    $cur_post_custom_keys = get_post_custom_keys();
    if (($cur_post_custom_keys==null) || !in_array('copyright', $cur_post_custom_keys)){
        $mine_info = sprintf( '
                        <div class="postcopyright">
                        防君子不防也无法防小人的声明:
                        <br />
                        如未注明转载则均为crifan原创,对于原创文章,转载请注明出处:<a href="%1$s" title="%2$s"><strong>%3$s - %4$s</strong></a>
                        <br />
                        本文链接:<a href="%5$s" title="%6$s"><strong>%7$s</strong></a>
                        </div>',
                        get_bloginfo('siteurl'), get_bloginfo('name'), get_bloginfo('name'), get_bloginfo('description'), get_permalink(), the_title_attribute( 'echo=0' ), the_title_attribute( 'echo=0' ));
        $output = $mine_info;
    }
    else {
        $copyright_value = get_post_custom_values('copyright');
        $other_info = sprintf( '
                        <div class="postcopyright">
                        本文由crifan转载自:<a target="_blank" rel="nofollow" href="%1$s" >%2$s</a>
                        <br />
                        本文链接:<a href="%3$s" title="%4$s">%5$s</a>
                        </div>',
                        $copyright_value[0], $copyright_value[0], get_permalink(), the_title_attribute( 'echo=0' ), the_title_attribute( 'echo=0' ));
        $output = $other_info;
    }

    return $output;
}

转载请注明:在路上 » 【已解决】wordpress中出现 Warning: in_array() expects parameter 2 to be array, null given in

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (1)

  1. 完美解决了我的问题
    8年前 (2016-07-28)回复
88 queries in 0.142 seconds, using 22.13MB memory