Function and Local variable doesn’t work inside another function

Something is really strange or am I doing something wrong, I can’t figure it out. When I add the variable $submenu in the wp_nav_menu args it doesn’t work. See the examples below.

Error Message

Read More

Strict Standards: Only variables should be passed by reference in

This Works

$args = array(
  'menu_id' => 'sidebar-menu',
  'theme_location' => 'primary',
  'submenu' => 'About Us'
);
wp_nav_menu( $args );

This doesn’t work

$args = array(
'menu_id' => 'sidebar-menu',
'theme_location' => 'primary',
        'submenu' => get_top_level_title()
    );
    wp_nav_menu( $args );

This function gets the top most parent title nothing fancy.

 function get_top_level_title(){
    global $post;
    $category = get_the_category( $post->ID );
    $parent_title = get_the_title( navz_top_most_parent_page_id() );
    if( is_single() ){
        $category = get_the_category( $post->ID );
        $post->post_parent = $category[0]->term_id;
        return get_cat_name($category[0]->category_parent);
    } else if( $parent_title and !is_category() ){
        return $parent_title;
    } else {
        $cat_obj = get_category( get_query_var( 'cat' ) );
        $parent = $cat_obj->parent;
        return get_cat_name($parent);
    }
}

This code gets the function above into a local variable and passess it to the wp_nav_menu to get the submenu.

   $submenu = get_top_level_title();
    $args = array(
      'menu_id' => 'sidebar-menu',
      'theme_location' => 'primary',
      'submenu' => $submenu
    );
    wp_nav_menu( $args );

Related posts