【问题标题】:Getting this error "Trying to get property of non-object" for a function为函数获取此错误“尝试获取非对象的属性”
【发布时间】:2017-12-19 11:19:35
【问题描述】:

这是一个删除一些横幅的函数,我知道错误发生在 if ( !in_array( $current_screen->post_type, $post_types ) ) 周围,但我不知道如何正确修复它。

function lsx_tec_disable_lsx_banner( $disabled ) {
    global $current_screen;
    $post_types = apply_filters( 'tribe_is_post_type_screen_post_types', Tribe__Main::get_post_types() );
    if ( ! in_array( $current_screen->post_type, $post_types ) ) {
        $disabled = true;
    }
    if ( is_null( $id ) && false !== strpos( $current_screen->id, 'tribe' ) ) 
    {
        $disabled = true;
    }
    if ( is_single() && tribe_is_event() ) {
        $disabled = true;
    }
    return $disabled;
}

【问题讨论】:

  • 这一行有问题 "if ( !in_array( $current_screen->post_type, $post_types ) ) {"
  • 有什么问题?我尝试了一些东西,但得到了错误
  • 你可以尝试打印 $current_screen->post_type 和 $post_types 吗?你可以看到问题。您已尝试从非对象中获取数据。
  • $current_screen 可能没有定义。该变量是在哪里创建的,是在全局范围内创建的吗?

标签: php wordpress function object plugins


【解决方案1】:

请检查下面的链接并更改您的代码。

https://codex.wordpress.org/Function_Reference/get_current_screen

<?php

    $current_screen = get_current_screen(); // use this instead of global $current_screen;


?>

【讨论】:

  • 如果我这样定义变量,它会破坏我的网站。
  • 请执行 var_dump( $current_screen ) 并检查结果。
  • 我得到了一些 NULL
猜你喜欢
  • 2015-09-22
  • 1970-01-01
  • 1970-01-01
  • 2017-06-04
  • 2021-08-05
  • 2016-09-10
  • 2018-09-26
  • 1970-01-01
  • 2018-01-16
相关资源
最近更新 更多