我对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()
不返回值,这可能是原因
我想我终于弄清楚了。当然,当我终于放弃并寻求帮助的那一刻,我就知道了!我更改了这条线:
$bee_authorid = $bee_temppost->post_author;
:
$bee_authorid = get_post_field( 'post_author', get_the_id() );
我猜在某些页面上,帖子可能不会加载该行返回null而不是作为对象。