Comment accéder à la "description" d'un élément de menu?
-
-
Pourquoin'utilisez-vouspas l'extrait depage?Why don’t you use the page excerpt?
- 0
- 2013-01-06
- fuxia
-
parce queje dois aussi l'utiliser dans lemenu,lorsque wp_nav_menuest appelé.J'ai unmarcheurpersonnalisé.because I need to use it in the menu as well, when wp_nav_menu is called. I have a custom walker.
- 0
- 2013-01-06
- Claire
-
@toscho Il s'avère queje dois utiliser l'extraitmaintenant.Pourriez-vousjeter un oeil à [cette question] (http://wordpress.stackexchange.com/questions/78723/how-to-get-the-excerpt-in-nav-menu-walker) s'il vousplaît?@toscho As it turns out I need to use the excerpt this now. Could you take a look at [this question](http://wordpress.stackexchange.com/questions/78723/how-to-get-the-excerpt-in-nav-menu-walker) please?
- 0
- 2013-01-08
- Claire
-
2 réponses
- votes
-
- 2013-01-06
Jen'aimepas l'idée d'analyser ànouveau les éléments demenu. Comme solution alternative,je suggère de stocker la description lors de lapremièreexécution:
add_filter( 'walker_nav_menu_start_el', 'wpse_78483_get_current_items_description', 10, 2 ); /** * Get nav items description. * * @wp-hook walker_nav_menu_start_el * @param string $item_output * @param object $item * @return string */ function wpse_78483_get_current_items_description( $item_output = NULL, $item = NULL ) { static $desc = ''; // The function is NOT called during nav menu rendering, but later. if ( 'walker_nav_menu_start_el' !== current_filter() ) return $desc; // The function is called during wp_nav_menu(). // description is set if ( ! empty ( $item->description ) // and an URL is available and ! empty ( $item->url ) // and it is the current page and parse_url( $item->url, PHP_URL_PATH ) === $_SERVER['REQUEST_URI'] ) { // copy the description into our static internal variable $desc = $item->description; // remove this filter, it is not needed anymore remove_filter( 'walker_nav_menu_start_el', __FUNCTION__ ); } // return unchanged item markup return $item_output; }
Explication
Lafonctionfait deux choses:
- Il agit comme unfiltre appelé à l'intérieur de
wp_nav_menu()
. Ici,ilest appeléjusqu'à ce qu'il atteigne lapageen cours. Ensuite,la descriptionest stockéeen interne dans$desc
. - Il agit comme ungetterpour la description: si vous appelez cettefonction sansparamètre après lemenu denavigation a été rendu,vous obtenez la valeur de la description,s'il yen a une.
L'inconvénientest que celane fonctionneraitpaspour un appel demenutroptard,dans unpied depageparexemple.
L'avantage: vousgagnez dutemps.Vouspouvez obtenir la descriptionplustard àtoutmomenten appelant lafonction sansparamètre:
print wpse_78483_get_current_items_description();
Enguise de suivi,voici une deuxièmefaçon de l'utiliser:
$desc = wpse_78483_get_current_items_description(); if ( empty ( $desc ) ) { the_excerpt(); } else { print wpautop( $desc ); }
Astuce supplémentaire: vouspouvez activer laboîte de l'éditeur d'extraitspour lespages:
add_action( 'wp_loaded', 'wpse_78483_page_excerpt' ); function wpse_78483_page_excerpt() { add_post_type_support( 'page', 'excerpt' ); }
I don’t like the idea to parse the menu items again. As an alternative solution I suggest to store the description during the first run:
add_filter( 'walker_nav_menu_start_el', 'wpse_78483_get_current_items_description', 10, 2 ); /** * Get nav items description. * * @wp-hook walker_nav_menu_start_el * @param string $item_output * @param object $item * @return string */ function wpse_78483_get_current_items_description( $item_output = NULL, $item = NULL ) { static $desc = ''; // The function is NOT called during nav menu rendering, but later. if ( 'walker_nav_menu_start_el' !== current_filter() ) return $desc; // The function is called during wp_nav_menu(). // description is set if ( ! empty ( $item->description ) // and an URL is available and ! empty ( $item->url ) // and it is the current page and parse_url( $item->url, PHP_URL_PATH ) === $_SERVER['REQUEST_URI'] ) { // copy the description into our static internal variable $desc = $item->description; // remove this filter, it is not needed anymore remove_filter( 'walker_nav_menu_start_el', __FUNCTION__ ); } // return unchanged item markup return $item_output; }
Explanation
The function does two things:
- It acts as a filter called inside of
wp_nav_menu()
. Here, it is called until it hits the current page. Then the description is stored internally in$desc
. - It acts as a getter for the description: If you call this function without parameter after the navigation menu has been rendered you get the value of the description, if there is one.
The downside is: it would not work for a menu call too late, in a footer for example.
The advantage: you save time.You can get the description later any time by calling the function without a parameter:
print wpse_78483_get_current_items_description();
As a follow-up, here is a second way to use it:
$desc = wpse_78483_get_current_items_description(); if ( empty ( $desc ) ) { the_excerpt(); } else { print wpautop( $desc ); }
Extra tip: You can enable the excerpt editor box for pages:
add_action( 'wp_loaded', 'wpse_78483_page_excerpt' ); function wpse_78483_page_excerpt() { add_post_type_support( 'page', 'excerpt' ); }
-
Je vous remercie.Jene comprendspas vraiment comment çamarchemais ça lefaitmerci!thank you. I don't really understand how it works but it does so thanks!
- 0
- 2013-01-06
- Claire
-
@Nicola Désolé,vous avez raison.J'aifait unemise àjour avec unemeilleureexplicationet des documentsen ligne.@Nicola Sorry, you are right. I have made an update with a better explanation and inline docs.
- 1
- 2013-01-06
- fuxia
-
merci,j'aimis àjourma questionpourmontrer queje veux quetoutes lespages stockent letexte alternatif dans unextraitthanks, I updated my question to show that I want all pages to store the alt text in excerpt
- 0
- 2013-01-08
- Claire
-
- 2013-01-06
J'aitrouvé la réponse sur un site Webmisen cachepar Google.
Donc,pour accéder à la description de l'élément denavigation de lapage actuelle,appelez simplement lafonction
echo wps_get_menu_description()
function wps_get_menu_description( ) { global $post; // Default $defaults = array( 'echo' => false, 'format' => '', 'description' => '', 'location' => 'primary', 'classes' => 'post-description' ); $args = wp_parse_args( $args, $defaults ); extract( $args , EXTR_SKIP ); // Get menu $menu_locations = get_nav_menu_locations(); $nav_items = wp_get_nav_menu_items( $menu_locations[ $location ] ); // Cycle through nav items foreach ( $nav_items as $nav_item ) { if ( ( is_page() || is_single() || is_archive() ) && ( $nav_item->object_id == $post->ID ) ) { $description = $nav_item->description; } } $output = $description; return $output; }
Found the answer on a google cached website.
So to access the current page's navigation item description - just call the function
echo wps_get_menu_description()
function wps_get_menu_description( ) { global $post; // Default $defaults = array( 'echo' => false, 'format' => '', 'description' => '', 'location' => 'primary', 'classes' => 'post-description' ); $args = wp_parse_args( $args, $defaults ); extract( $args , EXTR_SKIP ); // Get menu $menu_locations = get_nav_menu_locations(); $nav_items = wp_get_nav_menu_items( $menu_locations[ $location ] ); // Cycle through nav items foreach ( $nav_items as $nav_item ) { if ( ( is_page() || is_single() || is_archive() ) && ( $nav_item->object_id == $post->ID ) ) { $description = $nav_item->description; } } $output = $description; return $output; }
Où vouspouvez ajouter votremenu dans sous apparence>menus,j'ai des descriptions dedans. Dansmapage,je veuxpouvoirfaire écho à cette description. Pas dans lemenu,mais dansmapage. Commentpuis-je accéder à cesinformations?
MODIFIER:
@toscho
Commentmodifiermonmarcheurpersonnalisé? L'objet $item a-t-il accès à l'extrait depage?