wp_nav_menu_items filter :: custom menus fall outside ul class

The problem are these lines:

$items_icon = get_template_part( 'templates/topbar', 'rolecart' ); 
$items_icon = get_template_part( 'templates/topbar', 'roledashboard' );

where you expect get_template_part() to return the output, but it doesn’t, because it’s basically a wrapper for require() and require_once() calls.

This means the template parts are not part of the returned filter value.

I agree that the get_ prefix is confusing here!

It would make more sense to have it the_template_part() instead 😉

Here’s some hacking with output buffering. Let’s use the wpse_return_ prefix it emphasize that it returns the output:

if( ! function_exists( 'wpse_return_template_part' ) )
{
    function wpse_return_template_part( $first="", $second = '' )
    {
        ob_start();
        get_template_part( $first, $second );
        return ob_get_clean();
    }
}

and then

if( function_exists( 'wpse_return_template_part' ) )
    $items_icon = wpse_return_template_part( 'templates/topbar', 'rolecart' );