Fait vivre votre communication

Webmaster à temps partagé - 01 - 71 - 69 - 39
10 impasse des Acacias
Berzé-la-Ville
Bourgogne
71960
France

Setting up a single menu in WordPress

Let’s keep it simple this time

As time goes by, WordPress gets more powerful, heavier and complicated. Last time I installed a WordPress theme, I almost lost one hour with the menu settings. The administration panel displayed several choices, in terms of ‘menu name’ and ‘menu location’. I did not have the need for two menus or more. Let alone the time. What’s more, multiple menus can confuse our customers.

So I said to myself :

Next Monday (mondays are development days), have a look and make sure you come up with : an understanding of how it works + the code to output a single and simple menu.

How does it works ?

As usual, functions.php plays a great role in registering the fact that we want a cutomizable menu. Therefore, functions.php is the first file to be looked after. For the record, it took me 5 minutes to figure out that we have two functions that look pretty similar in writing : register_nav_menu and register_nav_menus. See it ? Then we open nav.php and set up all the parameters of the menu. Very convenient I must say.

Files

functions.php

function register_my_menu() {
	register_nav_menu('header-menu',__(''));
	}
	add_action( 'init', 'register_my_menu' );
;

nav.php


‹div id="access" role="navigation"›
‹?php

			$defaults = array(
				'theme_location'  => 'header-menu', 	// must be consistent with theme_location registration in functions.php
				'menu'            => '', 				// name of the menu that you set up in the admin panel, default is none
				'container'       => false, 			// false for no container
				'container_class' => '',				// default is menu-{menu slug}-container
				'container_id'    => '',				// id applied to the container, default is none
				'menu_class'      => '',				// class applied to the menu, default is none
				'menu_id'         => '',				// id applied to the menu, default is none
				'echo'            => true,				// wether to echo the menu or return it; default is true
				'fallback_cb'     => 'wp_page_menu',
				'before'          => '',				// output text before each  of the link; will NOT be part of the link; default is none
				'after'           => '',				// output text after each  of the link; will NOT be part of the link; default is none
				'link_before'     => '',				// output text right after opening ; will BE be part of the link; default is none
				'link_after'      => '',				// output text before closing ; will BE be part of the link; default is none
				'items_wrap'      => '‹ul id="%1$s" class="%2$s"›%3$s‹/ul›',
				'depth'           => 0,					// How many levels of the hierarchy are to be included where 0 means all. -1 displays links at any depth and arranges them in a single, flat list.
				'walker'          => ''
			);

			wp_nav_menu( $defaults );

		?›
	
	‹/div›

Article écrit par Jean Sam Denis

jean-sam

Bonjour, je suis Jean-Sam, webmaster / community manager à temps partagé, auprès de PME installées en Bourgogne, en Franche-Comté et Rhône-Alpes. J'essaie d'apporter de la cohérence et de l'ambition aux projets internet, afin qu'ils soient durables et profitables.
Ma particularité est de rester à proximité du client durant toute la durée de notre collaboration, en assurant la maintenance complète ou partielle du site (rédactionnel, photo, vidéo, newsletter, réseaux sociaux, référencement...).

Me joindre par mail ou me joindre par téléphone

De 2007 à 2017