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

时间:2015-04-10 03:49:32

标签: wordpress menu wordpress-theming

我的菜单代码

<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?

enter image description here

我得到的错误信息如下:

enter image description here

如果未设置菜单或指定

,如何使其标题变空或显示任何内容

1 个答案:

答案 0 :(得分:1)

根据错误判断我猜第78行是$ menu_obj-&gt;名称的回声?

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

<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