maintain dynamic menu items based on conditions in wp

22:54

Dynamic Menu

Haere two ways to possible to achieve Dynamic Menu. They Are
  1. Dynamic Menu using add filter
  2. maintain 2 menu navigations

By Using Filter Hook `wp_get_nav_menu_items`

wp_get_nav_menu_items filter hook is used to change the default functionality based on the condition.
  1. home
  2. about us
  3. conditional page
  4. contact us
 
function unset_conditional_item_menu( $items, $args ) {
   foreach ( $items as $key => $item ) {
    
   if ( $item->object_id == 105 && is_user_logged_in()) unset( $items[$key] );
  }

  return $items;
 }
add_filter( 'wp_get_nav_menu_items', 'unset_conditional_item_menu', 10, 2 );

maintain 2 menu navigations

Please add 2 different menus in 'appearence --> menus' in wordpress dashboard. They are 1. login menu (id: 41) 2. not logged in menu (id: 42) please set one of the menu based on the condition.
if(is_user_logged_in()){
    $d_menu='41';       
  }else{
    $d_menu='42';
  } 
    $args = array(
  'theme_location'  => $d_menu,
  'menu_class'      => 'apus-menu-top',
  'fallback_cb'     => '',
  'menu_id'         => $d_menu,
   );
   wp_nav_menu($args);
     

No comments:

Powered by Blogger.