如何在wp-includes/nav-menu-template.php中为条件导航显示未定义属性清除警告?



我编写了一个函数,根据用户是否登录和如果登录了则根据其会员状态更改主导航。下面是函数:

function custm_navigation_args($args) {
if(is_user_logged_in()) { 
$memid = get_current_user_id();
if ($memid != 0) {
global $wpdb;
$mems = $wpdb->get_results("select statement that grabs all users with specific membership ids");
$memb_group = array();
ksort($memb_group, SORT_NUMERIC);
if (!empty($mems)) {
foreach ($mems as $key => $item) {
$memb_meta = get_object_vars($item);
foreach ($memb_meta as $meta_in => $membdetails) {
$memb_group[$item->user_id]['memship'] = $memb_meta['memberships'];
}
}
$membprof = '';
foreach($memb_group as $mmm) {
$membprof .= $mmm['memship'];
if (!isset( $args['theme_location']) || $args['theme_location'] == "") {
$args['theme_location'] = 'primary-menu';
}
if ($membprof == '2017' || $membprof == '2048') {
$args['menu'] = 36;
} else if ($membprof == '2031' || $membprof == '2050') {
$args['menu'] = 38;
} else if ($membprof == '44' || $membprof == '1557' || $membprof == '1936') {
$args['menu'] = 2;
} else if ($membprof == '2326' || $membprof == '536') {
$args['menu'] = 41;
} else if ($membprof == '2364' || $membprof == '2371') {
$args['menu'] = 42;
}                    
}
} else {
$args['menu'] = 37; //Logged Out Menu
}
}
return $args;
}
}
add_filter( 'wp_nav_menu_args', 'custm_navigation_args' );

该函数可以工作,但是每次用户登录时,错误日志都会更新并显示以下警告:

  • PHP:未定义属性$menu in ./wp-includes/nav-menu-template.php第125行
  • PHP:未定义属性./wp-includes/nav-menu-template.php中的$theme_location第129行
  • PHP:未定义属性./wp-includes/nav-menu-template.php中的$theme_location第134行
  • PHP:未定义属性./wp-includes/nav-menu-template.php中的$container175
  • PHP:未定义属性stdClass::$depth in ./wp-includes/nav-menu-template.php第228行
  • PHP:未定义属性./wp-includes/nav-menu-template.php中的$menu_class247

我验证wp_registered_nav_menus主题位置名是正确的。我可以删除定义theme_location的IF语句,但在错误日志中仍然得到相同的结果。同样有趣的是,错误日志更新了3次,所有的警告都是相同的。我正在使用Divi主题。

如何清除这些警告并保持函数按预期工作?

我只是能够回到这个问题,并自己解决了这个问题。如果您在error_log中看到任何与我在这里记录的相同的问题,这是我修复自己的代码所做的,并且它完全清除了日志中的警告/错误:

function custm_navigation_args($args) {
if(is_user_logged_in()) { 
$memid = get_current_user_id();
if ($memid != 0) {
global $wpdb;
$mems = $wpdb->get_results("select statement that grabs all users with specific membership ids");
$memb_group = array();
ksort($memb_group, SORT_NUMERIC);
if (!empty($mems)) {
foreach ($mems as $key => $item) {
$memb_meta = get_object_vars($item);
foreach ($memb_meta as $meta_in => $membdetails) {
$memb_group[$item->user_id]['memship'] = $memb_meta['memberships'];
}
}
$membprof = '';
foreach($memb_group as $mmm) {
$membprof .= $mmm['memship'];
//Removed - Not Needed
//if (!isset( $args['theme_location']) || $args['theme_location'] == "") {
//$args['theme_location'] = 'primary-menu';
//}
if ($membprof == '2017' || $membprof == '2048') {
$args['menu'] = 36;
} else if ($membprof == '2031' || $membprof == '2050') {
$args['menu'] = 38;
} else if ($membprof == '44' || $membprof == '1557' || $membprof == '1936') {
$args['menu'] = 2;
} else if ($membprof == '2326' || $membprof == '536') {
$args['menu'] = 41;
} else if ($membprof == '2364' || $membprof == '2371') {
$args['menu'] = 42;
}                    
}
//} else { //MOVED
//$args['menu'] = 37; //Logged Out Menu
}
//return $args; //MOVED
} else {
$args['menu'] = 37; //Logged Out Menu
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'custm_navigation_args' );

最新更新