未分配菜单位置时的 Wordpress 错误处理

Wordpress Error Handling when no menu location assigned

我的菜单代码

<h6><?php 
    $theme_location = 'Footer-Customers';
    $theme_locations = get_nav_menu_locations();
    $menu_obj = get_term( $theme_locations[$theme_location], 'nav_menu' );
                            echo $menu_obj->name;
?></h6>    
<?php 
     $footer_02 = wp_nav_menu( array('theme_location' => 'Footer-Business' ,
                                                        'container' => '',
                                                        'echo' => FALSE,
                                                        'fallback_cb' => '__return_false') 
                                            ); 
    if ( ! empty ( $footer_02) )
    {
         echo $footer_02;
    }   
    ?>

请问如何查看后端设置(/wp-admin)中是否有NO菜单分配给theme_location?

我得到的错误消息如下:

如果没有设置或分配菜单,如何使其标题为空或不显示

根据错误判断,我猜第 78 行是 $menu_obj->name?

的回显

如果是这样,您可以执行以下操作:

<h6><?php 
    $theme_location = 'Footer-Customers';
    $theme_locations = get_nav_menu_locations();
    $menu_obj = get_term( $theme_locations[$theme_location], 'nav_menu' );

    // Check whether the name property exists - will return true if property 
    // is defined but empty
    $bHaveName = property_exists ( $menu_obj , 'name' );

    // Now check that it is both true and has some length before accessing
    if($bHaveName == true && strlen($menu_obj->name) > 0) {
        echo $menu_obj->name;
    }
?></h6>  

参考:PHP Manual