php - 识别哪个项目是非对象(PHP 注意:尝试获取非对象的属性)
问题描述
我对 PHP 和 WordPress 比较陌生,这条错误消息“PHP 通知:尝试获取非对象的属性”一直困扰着我,我相信有一个简单的解决方法。任何人都可以扫描以下代码并让我知道此错误的根源是什么?我确信我已将其范围缩小到这段代码。提前感谢您的帮助!
// REDIRECT USERS IF THEY ARE IN THE WRONG SPOT
add_action ('template_redirect', 'bee_security');
function bee_security() {
// set the page that people end up on if they try to access the wrong page
$bee_redirecturl = '/private-page/home/';
// get basic user information
$bee_masteruserid = get_user_meta(get_current_user_id(), 'wpcf-user-masteruser', true);
$bee_temppost = get_post($post = get_the_id());
$bee_authorid = $bee_temppost->post_author;
// determine if the current post type is related to households
$bee_posttype_household = substr(get_post_type(), 0, 9);
if ( $bee_posttype_household == "household") {
$bee_posttype_household = true;
} else {
$bee_posttype_household = false;
}
// redirect the user if they are logged in and accessing the front page
if ( is_front_page() && is_user_logged_in() ) {
wp_redirect($bee_redirecturl);
exit;
// redirect the user if they try to access somebody else's househould besides their own
} elseif ( $bee_posttype_household == true ) {
if ( $bee_authorid != get_current_user_id() && $bee_authorid != $bee_masteruserid ) {
wp_redirect($bee_redirecturl);
exit;
}
// redirect the user if they try to make a review on someone else's behalf
} elseif ( get_the_id() == 347 ) {
$bee_childpost = get_post($_GET['childid']);
$bee_childauthor = $bee_childpost->post_author;
if ( $bee_childauthor != get_current_user_id() && $bee_childauthor != $bee_masteruserid ) {
wp_redirect($bee_redirecturl);
}
}
}
解决方案
以下值之一产生错误
$bee_authorid = $bee_temppost->post_author;
或者
$bee_childauthor = $bee_childpost->post_author;
get_post()
不返回值,这可能是原因
推荐阅读
- python - 使用 ipywidgets 为 pandas df 中的每一行分配一个复选框
- html - Angular2将来自服务器的html包含到一个div中
- javascript - GraphQL 的突变上的“意外的令牌 < 在 JSON 中的位置 0”
- flutter - 如何使用盒子阴影显示容器的高度,如抽屉(更多深度 z 轴)?
- visual-studio - 复制 +,, 没有按预期工作:文件被触摸但也被复制
- javascript - asp.net 代码不调用 javascript 函数
- java - 如何在 Java 中实现构建特定的注解保留
- php - 我无法在 Godaddy 服务器上使用 CURL
- amazon-web-services - DynamoDB 多对多等效
- excel - 如何在 VBA 中自动填充函数?