0

I am using the plugin PHP Browser Detection and I want to achieve different menus for different devices. I have set them up called primary and mobile.

Now, I want to switch those on used device. Something like this I have done and it is working fine:

  if ( is_tablet() ) {
    wp_nav_menu( array('menu' => 'primary' ) );
  }
  elseif ( is_mobile() ) {
    wp_nav_menu( array('menu' => 'mobile' ) );
  }
  else {
    wp_nav_menu( array('menu' => 'primary' ) );
  }

Now, I would like to go further, because the tablet's design looks nice in landscape view but bad in portrait mode.

So, I ask you, if there is a way to check the orientation (on tablets) and then go for it, meaning to change the menu from primary to mobile.

Something like that maybe:

  if ( is_tablet(portrait) ) {
  ...

Thank you very much, guys.

2
  • 4
    My opinion: go with responsive design or die. Never trust in server-side browser detection.
    – cybmeta
    Nov 11, 2014 at 15:55
  • I have created a responsive design, but I need a different menu (my second menu called 'mobile') on phones in general and on tablets in portrait mode only, because this menu has missing some items, which I do not want to display on those devices. HTML/CSS can not remove menu points/items from Wordpress' nav.
    – vega
    Nov 11, 2014 at 16:01

2 Answers 2

1

cybmeta is right you cannot trust server-side browser detection

<div class="portrait">
 wp_nav_menu( array('menu' => 'primary' ) );
</div>

<div class="mobile">
 wp_nav_menu( array('menu' => 'mobile' ) );
</div>

<div class="primary">
 wp_nav_menu( array('menu' => 'primary' ) );
</div>

and use media queries to hide and display menu

2
  • Please properly format your code next time. If you add code, highlight it and click {} just above the editor. This will place your code in a code block which will properly display your code. Thanks :-) Nov 11, 2014 at 16:14
  • @pieter thanx again for pointing out my mistake
    – ManuA
    Nov 11, 2014 at 16:19
1

@cybmeta is correct, you're really better off going with a responsive approach. You can hide specific items in wp_nav_menu using CSS. You could just create a menu with all the pages you want throughout the devices. An unmodified wp_nav_menu will actually provide each list item with a specific unique ID such as nav-menu-item-789. Knowing this you could inspect each list item, and using media queries you could hide or show entire list items based off IDs.

This is just an alternative to generating three separate wp_nav_menus and show / hide them as you go. Both of which should work with little effort.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.