Thursday, September 22, 2016

Wordpress muffin betheme conditional menu

Goal: Show different menu race by logged in user state.
When user is logged it will be show a different one.

After try several plugin, (but no one is working ) and reading worst discussion on muffin forum
i decided, reluctantly, to write some php function to solve issue.

In my scenario consider i'm using betheme with child theme.
Create menu to show when user will be loggedin, in my case call it: menu-loggedout

Insert on function.php file:
       
 
function my_wp_nav_menu_args( $args = '' ) {
 // Primary menu location
 if( 'main-menu' == $args['theme_location'] ) {
  if( is_user_logged_in() ) {
   $args['menu'] = 'menu-loggedout';
  } else {
   $args['menu'] = 'Main Menu';
  }

  return $args;
 }else{
  echo $args['theme_location'];
 }
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );

       
 
Save and now check on front-end site ... it work like a charm!

Consider, if something was wrong, to echoing $args['theme_location'] variable as you can discover quickly how muffin developer define ad store main menu name.

If this suggestion solve your day please leave a short like or follow me. :)