0

I want to have two different menus for desktop and mobile phone users.

So, I have created two places for two menus (primary, mobile) with this function:

functions.php:

function is_register_my_menus() {
register_nav_menus(
array(
  'primary' => __( 'primary' ),
  'mobile' => __( 'mobile' )  
)
);
}
add_action( 'init', 'is_register_my_menus' ); 

So far, so good. I could create another menu with one item left out and set that menu as "mobile".

Then I installed the Wordpress plugin PHP Browser Detection, which works just fine. With that I tried to have a switching/exchange of those two menus, depending on the device.

header.php:

<nav id="nav" role="navigation" class="gross">
        <div id="knopf"><a href="#"></a></div>
        <?php if ( is_mobile() ) { wp_nav_menu( array('menu' => 'mobile' ) ); } else { wp_nav_menu( array('menu' => 'primary' ) ); }; ?>
    </nav>

Unfortunately, it shows always the primary menu, regardless of computer/desktop or mobile device. Did I do something wrong in the if/else code? I use that on my posts to exchange post_thumbnail per device, as well, and there it is working fine.

1

There isn't an is_mobile() function to my knowledge. WordPress uses a wp_is_mobile() which is probably what you're looking for. So it should look like this:

<?php if ( wp_is_mobile() ) { wp_nav_menu( array('menu' => 'mobile' ) ); } else { wp_nav_menu( array('menu' => 'primary' ) ); }; ?>

EDIT

After reviewing your question further it seems like you have a plugin installed to browser sniff for you. It does look like it uses is_mobile() but since I am unfamiliar with the plugin I would suggest going vanilla with wp_is_mobile() instead of using a plugin, who knows what could be wrong with the plugin or if the plugin is compatible with your installation.

FINAL EDIT

It turns out according to OP that the menu names were not matching.

  • Thank you, very much. I changed that bit, immediately, BUT it does not have any effect on the menu. It still shows the old menu (with "Portfolio" in that example). You can look over there, if you like: studio-scs.de/person ... just no change from desktop to mobile phone regarding the menu points. So, the plugin is not the issue, definitively. I am using it on the post_thumbnails without any problem. – vega Nov 10 '14 at 19:59
  • @vega I've viewed your website and it seems to show the correct menu on mobile versions ( The drop down menu, top right corner, as the menu.gif ). Is this what you're referring to or is there a different menu not showing on mobile? How are you testing this? – Howdy_McGee Nov 10 '14 at 20:02
  • No, no, this is just the hiding I coded. I am talking about DIFFERENT MENU CONTENT. The desktop menu has those points: "Portfolio", "Journal", ... etc. The mobile phone shall have the same points, BUT missing one: "Portfolio". That is why I created two menus and they should be exchanged per need/device. The second menu (mobile) I have set without the point "Portfolio" to achieve that. – vega Nov 10 '14 at 20:04
  • Strange, the code above should work as expected. Are you sure in Appearance -> Nav Menus The Mobile Menu is exactly named "mobile" (correct case and everything) ? – Howdy_McGee Nov 10 '14 at 20:06
  • 1
    Yeah, I am no fan of plugins either, although this one has good references. Anyway, thank you a lot. :) – vega Nov 10 '14 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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