Personnaliser uniquement un menu spécifique à l'aide du crochet "wp_nav_menu_items"?
2 réponses
- votes
-
- 2010-09-23
Pour ajouter uniquement la zone de recherchepersonnalisée aumenuprincipal,vouspouvezpasser le deuxièmeparamètrefournipar lefiltre wp_nav_menu_itemset vérifier si letheme_locationest l'emplacementprincipal
add_filter('wp_nav_menu_items','search_box_function', 10, 2); function search_box_function( $nav, $args ) { if( $args->theme_location == 'primary' ) return $nav."<li class='menu-header-search'><form action='http://example.com/' id='searchform' method='get'><input type='text' name='s' id='s' placeholder='Search'></form></li>"; return $nav; }
To only add the custom search box to the main menu you could pass the second parameter provided by the wp_nav_menu_items filter and check if the theme_location is the primary location
add_filter('wp_nav_menu_items','search_box_function', 10, 2); function search_box_function( $nav, $args ) { if( $args->theme_location == 'primary' ) return $nav."<li class='menu-header-search'><form action='http://example.com/' id='searchform' method='get'><input type='text' name='s' id='s' placeholder='Search'></form></li>"; return $nav; }
-
merci,c'est super ...juste une questionpour les débutants - quefait le «10,2» dans le code?thanks, that's great... just a newbie question - what does the `10, 2` do in the code?
- 3
- 2010-09-23
- cannyboy
-
«10»est lapriorité (dixest la valeurpar défaut),«2»est lenombre d'arguments acceptéspar lafonction quenous accrochonspourfiltrer.`10` is priority (ten is default), `2` is number of arguments that function we are hooking to filter accepts.
- 5
- 2010-09-23
- Rarst
-
Notez que depuis WP 3.8,la variableest $ args->menu au lieu de $ args->theme_locationNote that as of WP 3.8, the variable is $args->menu instead of $args->theme_location
- 1
- 2014-03-26
- Alain Jacomet Forte
-
@AlainJacometForte Enfait,$ args->menu renvoie l'IDnumérique dumenu,tandis que $ args->theme_location renvoie la chaîne,donc cette réponseesttoujours correctetelle qu'elleest.Rienn'a changé si vous comparez les chaînes.@AlainJacometForte Actually, $args->menu returns the numeric ID of the menu, while $args->theme_location returns the string, so this answer is still correct the way it is. Nothing has changed if you are comparing the strings.
- 0
- 2014-05-25
- Marcus
-
L'utilisation de $ args->menu au lieu de $ args->theme_location afait cetravailpourmoien utilisant 4.3Using $args->menu instead of $args->theme_location made this work for me using 4.3
- 0
- 2015-08-21
- shaunsantacruz
-
- 2012-12-19
Une autreméthode consiste à ajouter le slug demenu aufiltre wp_nav_menu_items.
Parexemple,disons que vous avez unmenunommé En-têteet que vous vouleztoujours que cemenu (qu'il soit attaché à unemplacement dethème ounon) affiche une zone de recherche.Vouspouvez lefaireen ajoutant le slug demenu,dans ce cas
header
,aufiltre.Lenouveaufiltre serait le suivant:
add_filter ('wp_nav_menu_header_items','search_box_function');
Notez lapartie
header
dunouveaufiltre.Celaindique à WordPress dans quelmenu ajouter lafonction.Cen'est qu'unemanière différente d'aborder votreproblème actuel.
An alternative method of doing this is adding the menu slug to the wp_nav_menu_items filter.
For example, let's say you have a menu named Header and you always want this menu (whether it's attached to a theme location or not) to display a search box. You can do so by adding the menu slug, in this case
header
, to the filter.The new filter would be as follows:
add_filter( 'wp_nav_menu_header_items', 'search_box_function' );
Notice the
header
portion of the new filter. This tell WordPress what menu to add the function to.This is just one different way to approach your current problem.
-
Voyez l'article de @oshi sur cettepage,qui vousest adressé.See the item from @oshi on this page, which is addressed to you.
- 0
- 2013-01-02
- halfer
Grâce à unpeu d'aideici,j'ai réussi à ajouter un champ de recherchepersonnalisé àmonmenuprincipal ...en l'ajoutant auxfunctions.php demonthème
Cependant,j'aimaintenant ajouté un autremenu àmettre dans lepied depage,mais le champ de rechercheest également ajouté à celui-ci. Comment ajouter le champ de recherche aumenuprincipal uniquement?
Mon code d'enregistrement desmenusest:
..et le codepour afficher lemenu secondaireest: