{"id":121692,"date":"2013-09-20T11:30:19","date_gmt":"2013-09-20T15:30:19","guid":{"rendered":"http:\/\/wpmu.org\/?p=121692"},"modified":"2017-05-25T07:40:42","modified_gmt":"2017-05-25T07:40:42","slug":"how-to-create-awesome-responsive-menu","status":"publish","type":"post","link":"https:\/\/wpmudev.com\/blog\/how-to-create-awesome-responsive-menu\/","title":{"rendered":"How to Create a Responsive Menu for Your WordPress Theme"},"content":{"rendered":"<p>Making your WordPress site accessible on mobile devices starts \u2013 but doesn&#8217;t end \u2013 with the installation of a responsive theme.<\/p>\n<p>Navigation is hugely important to the user experience of any site and forcing menus designed for much larger devices onto mobile users could undo all the good work of the responsive design.<\/p>\n<p>Here&#8217;s five simple steps to add mobile-specific menus to your responsive theme and provide an awesome viewing experience for your mobile visitors.<\/p>\n<figure id=\"attachment_121874\" class=\"wp-caption alignnone\" data-caption=\"true\"><a rel=\"lightbox[121692]\" class=\"blog-thumbnail\" href=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2013\/09\/shipinabottle.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-121874\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2013\/09\/shipinabottle.jpg\" alt=\"Don't force your desktop navigation into a small screen\" width=\"800\" height=\"300\" \/><\/a><figcaption class=\"wp-caption-text\">Confined spaces need alternative solutions.<\/figcaption><\/figure>\n<figure id=\"attachment_121703\" class=\"wp-caption alignright\" data-caption=\"true\"><a rel=\"lightbox[121692]\" class=\"blog-thumbnail\" href=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2013\/09\/mobile-menu.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"size-ratio-2-3 wp-image-121703\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2013\/09\/mobile-menu-312x468.png\" alt=\"Screenshot from a smartphone showing WPMU.org's mobile menu\" width=\"312\" height=\"468\" \/><\/a><figcaption class=\"wp-caption-text\">WPMU.org has a specific menu just for mobile devices.<\/figcaption><\/figure>\n<p>There are number of reasons why smaller screens deserve their own navigation:<\/p>\n<ul>\n<li><strong>Limited Real Estate<\/strong>: a long menu may simply not fit on a mobile screen. Menus on smaller devices need to shorter and deeper than their desktop counterparts.<\/li>\n<li><strong>Changing the Priority of Pages for Mobile Users<\/strong>: it may be that mobile users have a different set of popular pages to desktop users (check your analytics now!) and so it makes sense to give these pages a higher priority in your navigation.<\/li>\n<li><strong>Providing Mobile-only Versions of Pages<\/strong>: you may want to deliver a different version of the same page for mobile users and changing the page link in the navigation allows you to do this.<\/li>\n<\/ul>\n<p>There are two options for providing platform-specific menus. One is to determine the platform from the browser making the request and then output the appropriate menu. The other is to output all menus and then make use of <em>@media<\/em> queries in the stylesheet to only show the appropriate menu. Whilst the <em>@media<\/em> query approach does mean outputting HTML that may not be used, it is consistent with responsive design and so that\u2019s the option we are going to look at here. We\u2019ll be working with the TwentyThirteen theme shipped with WordPress 3.6.<\/p>\n<h2>Step 1 \u2013 Create a Child Theme<\/h2>\n<p>If you don\u2019t already have a <a title=\"Read about child themes on the WordPress Codex\" href=\"http:\/\/codex.wordpress.org\/Child_Themes\" rel=\"noopener\" target=\"_blank\">child theme<\/a>\u00a0then create one now!\u00a0Apart from being best practice, it makes customisation much easier, keeps the original theme in tact and will enable you to update the original theme without losing all your modifications. Copy across the <em>header.php<\/em> from your current theme and create an empty <em>functions.php<\/em>.<\/p>\n<h2>Step 2 \u2013 Add a New Menu Location<\/h2>\n<p>Adding a menu location to house our mobile menu will make the solution far more flexible. Working with a menu location rather than with a specific menu will enable new menus to be assigned to the location without any need to update the stylesheet. To add a new menu location, add the following to <em>functions.php<\/em>:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfunction extra_setup() {\r\nregister_nav_menu ('primary mobile', __( 'Navigation Mobile', 'twentythirteen' ));\r\n}\r\nadd_action( 'after_setup_theme', 'extra_setup' );\r\n\r\n<\/pre>\n<figure id=\"attachment_121696\" class=\"wp-caption alignright\" data-caption=\"true\"><a rel=\"lightbox[121692]\" class=\"blog-thumbnail\" href=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2013\/09\/menu_settings.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-121696 \" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2013\/09\/menu_settings-312x154.png\" alt=\"Screen snippet showing additional menu location in Theme Locations\" width=\"312\" height=\"154\" \/><\/a><figcaption class=\"wp-caption-text\">Adding a mobile menu to the theme locations<\/figcaption><\/figure>\n<p>Now when you go to <strong>Appearance &gt; Menus<\/strong>\u00a0in the backend of your WordPress installation, you\u2019ll now see two menu locations as possible options for a menu. Create a menu and assign it to the new location so that you have something to test with.<\/p>\n<h2>Step 3 \u2013 Add the New Location to the Header<\/h2>\n<p>Open up <em>header.php<\/em>, find the existing call to <em>wp_nav_menu<\/em> and add the following immediately underneath:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php wp_nav_menu( array( 'theme_location' =&gt; 'primary mobile', 'menu_class' =&gt; 'nav-menu' ) ); ?&gt;;\r\n\r\n<\/pre>\n<p>This call generates the HTML for our new menu location.<\/p>\n<h2>Step 4 \u2013 Set the CSS classes for the menus<\/h2>\n<p>To show the appropriate menu, we are going to make use of <em>@media<\/em> queries to toggle the displaying of the primary and mobile menus.<\/p>\n<p>By default, WordPress wraps menus in a <em>div tag<\/em> with a class name derived from the menu name. Whilst we could simply use these class names in our stylesheet, that would mean having to update the stylesheet every time a different menu is assigned to a location. To maximize flexibility we will set our own more generic class name for the menu container. We can do this very simply by using the <em>wp_nav_menu_args<\/em> filter.<\/p>\n<p>Go back to your <em>functions.php<\/em> file and add the following:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\r\nfunction set_container_class ($args) {\r\n$args&#x5B;'container_class'] = str_replace(' ','-',$args&#x5B;'theme_location']).'-nav'; return $args;\r\n}\r\nadd_filter ('wp_nav_menu_args', 'set_container_class')\r\n\r\n<\/pre>\n<p>All we are doing here is setting the <em>container_class<\/em> to the theme location (replacing spaces with hyphens) and adding \u2018-nav\u2019. We have registered the locations <em>primary<\/em> and <em>primary mobile<\/em> so WordPress will now output our 2 menus wrapped in <em>&lt;div class=&#8221;primary-nav&#8221;&gt;<\/em> and <em>&lt;div class=&#8221;primary-mobile-nav&#8221;&gt;<\/em> elements respectively.<\/p>\n<h2>Step 5 \u2013 Amend the Stylesheet to Control Menu Display<\/h2>\n<p>Our final step is to add the styling to only show the appropriate menu. Open <em>styles.css<\/em> and add the following:<\/p>\n<pre class=\"brush: css; title: ; notranslate\" title=\"\">\r\n.primary-mobile-nav {\r\ndisplay: none;\r\n}\r\n@media (max-width: 643px){\r\n.primary-nav {\r\ndisplay: none;\r\n}\r\n.primary-mobile-nav {\r\ndisplay: block;\r\n}\r\n}\r\n\r\n<\/pre>\n<p>Generally, we don\u2019t want the mobile menu to show so we\u2019ll hide it by default by setting its <em>display<\/em> attribute to <em>none<\/em>.<\/p>\n<p>To ensure that we only show the mobile menu (and hide the primary menu) when we get below a certain screen-size, we place the display statements within an appropriate <em>@media<\/em> query. For the TwentyThirteen theme it\u2019s at 643px but you\u2019ll need to check your theme\u2019s stylesheet to see which <em>@media<\/em> query activates the small menu.<\/p>\n<p>Of course, you can add any number of menus and target multiple screen-sizes just by adding more menus in steps two and three and updating the stylesheet with the appropriate <em>@media<\/em> queries.<\/p>\n<p>Just five quick and easy steps to add awesome mobile menus to your responsive WordPress theme and provide your non-desktop users with a far better and more tailored experience.<\/p>\n<p><strong>What other steps do you take to improve the mobile experience for your visitors? Do you even take any steps? Tell us in the comments below.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Updating a responsive theme to allow mobile-specific menus is quick and easy and will provide significant benefits for both you and your mobile visitors.<\/p>\n","protected":false},"author":262394,"featured_media":121874,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"blog_reading_time":"","wds_primary_category":0,"wds_primary_tutorials_categories":0,"footnotes":""},"categories":[263],"tags":[2395],"tutorials_categories":[],"class_list":["post-121692","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-mobile"],"_links":{"self":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/121692","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/users\/262394"}],"replies":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/comments?post=121692"}],"version-history":[{"count":5,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/121692\/revisions"}],"predecessor-version":[{"id":195211,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/121692\/revisions\/195211"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media\/121874"}],"wp:attachment":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media?parent=121692"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/categories?post=121692"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tags?post=121692"},{"taxonomy":"tutorials_categories","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tutorials_categories?post=121692"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}