WordPress Error Handling when no menu location assigned

My menu code

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

May I know how to check if there is NO menu assigned to the theme_location in backend setting(/wp-admin)?

Read More

enter image description here

The error msg i get is as follow:

enter image description here

How to make its title become empty or show nothing if the menu is not set or assign

Related posts

Leave a Reply

1 comment

  1. Judging by the error I’m guessing line 78 is the echoing of $menu_obj->name?

    If so you can do something like the following:

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

    Reference: PHP Manual