[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Nav Menu API: Template functions 4 * 5 * @package WordPress 6 * @subpackage Nav_Menus 7 * @since 3.0.0 8 */ 9 10 /** Walker_Nav_Menu class */ 11 require_once ABSPATH . WPINC . '/class-walker-nav-menu.php'; 12 13 /** 14 * Displays a navigation menu. 15 * 16 * @since 3.0.0 17 * @since 4.7.0 Added the `item_spacing` argument. 18 * @since 5.5.0 Added the `container_aria_label` argument. 19 * 20 * @param array $args { 21 * Optional. Array of nav menu arguments. 22 * 23 * @type int|string|WP_Term $menu Desired menu. Accepts a menu ID, slug, name, or object. 24 * Default empty. 25 * @type string $menu_class CSS class to use for the ul element which forms the menu. 26 * Default 'menu'. 27 * @type string $menu_id The ID that is applied to the ul element which forms the menu. 28 * Default is the menu slug, incremented. 29 * @type string $container Whether to wrap the ul, and what to wrap it with. 30 * Default 'div'. 31 * @type string $container_class Class that is applied to the container. 32 * Default 'menu-{menu slug}-container'. 33 * @type string $container_id The ID that is applied to the container. Default empty. 34 * @type string $container_aria_label The aria-label attribute that is applied to the container 35 * when it's a nav element. Default empty. 36 * @type callable|false $fallback_cb If the menu doesn't exist, a callback function will fire. 37 * Default is 'wp_page_menu'. Set to false for no fallback. 38 * @type string $before Text before the link markup. Default empty. 39 * @type string $after Text after the link markup. Default empty. 40 * @type string $link_before Text before the link text. Default empty. 41 * @type string $link_after Text after the link text. Default empty. 42 * @type bool $echo Whether to echo the menu or return it. Default true. 43 * @type int $depth How many levels of the hierarchy are to be included. 44 * 0 means all. Default 0. 45 * Default 0. 46 * @type object $walker Instance of a custom walker class. Default empty. 47 * @type string $theme_location Theme location to be used. Must be registered with 48 * register_nav_menu() in order to be selectable by the user. 49 * @type string $items_wrap How the list items should be wrapped. Uses printf() format with 50 * numbered placeholders. Default is a ul with an id and class. 51 * @type string $item_spacing Whether to preserve whitespace within the menu's HTML. 52 * Accepts 'preserve' or 'discard'. Default 'preserve'. 53 * } 54 * @return void|string|false Void if 'echo' argument is true, menu output if 'echo' is false. 55 * False if there are no items or no menu was found. 56 */ 57 function wp_nav_menu( $args = array() ) { 58 static $menu_id_slugs = array(); 59 60 $defaults = array( 61 'menu' => '', 62 'container' => 'div', 63 'container_class' => '', 64 'container_id' => '', 65 'container_aria_label' => '', 66 'menu_class' => 'menu', 67 'menu_id' => '', 68 'echo' => true, 69 'fallback_cb' => 'wp_page_menu', 70 'before' => '', 71 'after' => '', 72 'link_before' => '', 73 'link_after' => '', 74 'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>', 75 'item_spacing' => 'preserve', 76 'depth' => 0, 77 'walker' => '', 78 'theme_location' => '', 79 ); 80 81 $args = wp_parse_args( $args, $defaults ); 82 83 if ( ! in_array( $args['item_spacing'], array( 'preserve', 'discard' ), true ) ) { 84 // Invalid value, fall back to default. 85 $args['item_spacing'] = $defaults['item_spacing']; 86 } 87 88 /** 89 * Filters the arguments used to display a navigation menu. 90 * 91 * @since 3.0.0 92 * 93 * @see wp_nav_menu() 94 * 95 * @param array $args Array of wp_nav_menu() arguments. 96 */ 97 $args = apply_filters( 'wp_nav_menu_args', $args ); 98 $args = (object) $args; 99 100 /** 101 * Filters whether to short-circuit the wp_nav_menu() output. 102 * 103 * Returning a non-null value from the filter will short-circuit wp_nav_menu(), 104 * echoing that value if $args->echo is true, returning that value otherwise. 105 * 106 * @since 3.9.0 107 * 108 * @see wp_nav_menu() 109 * 110 * @param string|null $output Nav menu output to short-circuit with. Default null. 111 * @param stdClass $args An object containing wp_nav_menu() arguments. 112 */ 113 $nav_menu = apply_filters( 'pre_wp_nav_menu', null, $args ); 114 115 if ( null !== $nav_menu ) { 116 if ( $args->echo ) { 117 echo $nav_menu; 118 return; 119 } 120 121 return $nav_menu; 122 } 123 124 // Get the nav menu based on the requested menu. 125 $menu = wp_get_nav_menu_object( $args->menu ); 126 127 // Get the nav menu based on the theme_location. 128 $locations = get_nav_menu_locations(); 129 if ( ! $menu && $args->theme_location && $locations && isset( $locations[ $args->theme_location ] ) ) { 130 $menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] ); 131 } 132 133 // Get the first menu that has items if we still can't find a menu. 134 if ( ! $menu && ! $args->theme_location ) { 135 $menus = wp_get_nav_menus(); 136 foreach ( $menus as $menu_maybe ) { 137 $menu_items = wp_get_nav_menu_items( $menu_maybe->term_id, array( 'update_post_term_cache' => false ) ); 138 if ( $menu_items ) { 139 $menu = $menu_maybe; 140 break; 141 } 142 } 143 } 144 145 if ( empty( $args->menu ) ) { 146 $args->menu = $menu; 147 } 148 149 // If the menu exists, get its items. 150 if ( $menu && ! is_wp_error( $menu ) && ! isset( $menu_items ) ) { 151 $menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) ); 152 } 153 154 /* 155 * If no menu was found: 156 * - Fall back (if one was specified), or bail. 157 * 158 * If no menu items were found: 159 * - Fall back, but only if no theme location was specified. 160 * - Otherwise, bail. 161 */ 162 if ( ( ! $menu || is_wp_error( $menu ) || ( isset( $menu_items ) && empty( $menu_items ) && ! $args->theme_location ) ) 163 && isset( $args->fallback_cb ) && $args->fallback_cb && is_callable( $args->fallback_cb ) ) { 164 return call_user_func( $args->fallback_cb, (array) $args ); 165 } 166 167 if ( ! $menu || is_wp_error( $menu ) ) { 168 return false; 169 } 170 171 $nav_menu = ''; 172 $items = ''; 173 174 $show_container = false; 175 if ( $args->container ) { 176 /** 177 * Filters the list of HTML tags that are valid for use as menu containers. 178 * 179 * @since 3.0.0 180 * 181 * @param string[] $tags The acceptable HTML tags for use as menu containers. 182 * Default is array containing 'div' and 'nav'. 183 */ 184 $allowed_tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) ); 185 186 if ( is_string( $args->container ) && in_array( $args->container, $allowed_tags, true ) ) { 187 $show_container = true; 188 $class = $args->container_class ? ' class="' . esc_attr( $args->container_class ) . '"' : ' class="menu-' . $menu->slug . '-container"'; 189 $id = $args->container_id ? ' id="' . esc_attr( $args->container_id ) . '"' : ''; 190 $aria_label = ( 'nav' === $args->container && $args->container_aria_label ) ? ' aria-label="' . esc_attr( $args->container_aria_label ) . '"' : ''; 191 $nav_menu .= '<' . $args->container . $id . $class . $aria_label . '>'; 192 } 193 } 194 195 // Set up the $menu_item variables. 196 _wp_menu_item_classes_by_context( $menu_items ); 197 198 $sorted_menu_items = array(); 199 $menu_items_with_children = array(); 200 foreach ( (array) $menu_items as $menu_item ) { 201 /* 202 * Fix invalid `menu_item_parent`. See: https://core.trac.wordpress.org/ticket/56926. 203 * Compare as strings. Plugins may change the ID to a string. 204 */ 205 if ( (string) $menu_item->ID === (string) $menu_item->menu_item_parent ) { 206 $menu_item->menu_item_parent = 0; 207 } 208 209 $sorted_menu_items[ $menu_item->menu_order ] = $menu_item; 210 if ( $menu_item->menu_item_parent ) { 211 $menu_items_with_children[ $menu_item->menu_item_parent ] = true; 212 } 213 } 214 215 // Add the menu-item-has-children class where applicable. 216 if ( $menu_items_with_children ) { 217 foreach ( $sorted_menu_items as &$menu_item ) { 218 if ( isset( $menu_items_with_children[ $menu_item->ID ] ) ) { 219 $menu_item->classes[] = 'menu-item-has-children'; 220 } 221 } 222 } 223 224 unset( $menu_items, $menu_item ); 225 226 /** 227 * Filters the sorted list of menu item objects before generating the menu's HTML. 228 * 229 * @since 3.1.0 230 * 231 * @param array $sorted_menu_items The menu items, sorted by each menu item's menu order. 232 * @param stdClass $args An object containing wp_nav_menu() arguments. 233 */ 234 $sorted_menu_items = apply_filters( 'wp_nav_menu_objects', $sorted_menu_items, $args ); 235 236 $items .= walk_nav_menu_tree( $sorted_menu_items, $args->depth, $args ); 237 unset( $sorted_menu_items ); 238 239 // Attributes. 240 if ( ! empty( $args->menu_id ) ) { 241 $wrap_id = $args->menu_id; 242 } else { 243 $wrap_id = 'menu-' . $menu->slug; 244 245 while ( in_array( $wrap_id, $menu_id_slugs, true ) ) { 246 if ( preg_match( '#-(\d+)$#', $wrap_id, $matches ) ) { 247 $wrap_id = preg_replace( '#-(\d+)$#', '-' . ++$matches[1], $wrap_id ); 248 } else { 249 $wrap_id = $wrap_id . '-1'; 250 } 251 } 252 } 253 $menu_id_slugs[] = $wrap_id; 254 255 $wrap_class = $args->menu_class ? $args->menu_class : ''; 256 257 /** 258 * Filters the HTML list content for navigation menus. 259 * 260 * @since 3.0.0 261 * 262 * @see wp_nav_menu() 263 * 264 * @param string $items The HTML list content for the menu items. 265 * @param stdClass $args An object containing wp_nav_menu() arguments. 266 */ 267 $items = apply_filters( 'wp_nav_menu_items', $items, $args ); 268 /** 269 * Filters the HTML list content for a specific navigation menu. 270 * 271 * @since 3.0.0 272 * 273 * @see wp_nav_menu() 274 * 275 * @param string $items The HTML list content for the menu items. 276 * @param stdClass $args An object containing wp_nav_menu() arguments. 277 */ 278 $items = apply_filters( "wp_nav_menu_{$menu->slug}_items", $items, $args ); 279 280 // Don't print any markup if there are no items at this point. 281 if ( empty( $items ) ) { 282 return false; 283 } 284 285 $nav_menu .= sprintf( $args->items_wrap, esc_attr( $wrap_id ), esc_attr( $wrap_class ), $items ); 286 unset( $items ); 287 288 if ( $show_container ) { 289 $nav_menu .= '</' . $args->container . '>'; 290 } 291 292 /** 293 * Filters the HTML content for navigation menus. 294 * 295 * @since 3.0.0 296 * 297 * @see wp_nav_menu() 298 * 299 * @param string $nav_menu The HTML content for the navigation menu. 300 * @param stdClass $args An object containing wp_nav_menu() arguments. 301 */ 302 $nav_menu = apply_filters( 'wp_nav_menu', $nav_menu, $args ); 303 304 if ( $args->echo ) { 305 echo $nav_menu; 306 } else { 307 return $nav_menu; 308 } 309 } 310 311 /** 312 * Adds the class property classes for the current context, if applicable. 313 * 314 * @access private 315 * @since 3.0.0 316 * 317 * @global WP_Query $wp_query WordPress Query object. 318 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 319 * 320 * @param array $menu_items The current menu item objects to which to add the class property information. 321 */ 322 function _wp_menu_item_classes_by_context( &$menu_items ) { 323 global $wp_query, $wp_rewrite; 324 325 $queried_object = $wp_query->get_queried_object(); 326 $queried_object_id = (int) $wp_query->queried_object_id; 327 328 $active_object = ''; 329 $active_ancestor_item_ids = array(); 330 $active_parent_item_ids = array(); 331 $active_parent_object_ids = array(); 332 $possible_taxonomy_ancestors = array(); 333 $possible_object_parents = array(); 334 $home_page_id = (int) get_option( 'page_for_posts' ); 335 336 if ( $wp_query->is_singular && ! empty( $queried_object->post_type ) && ! is_post_type_hierarchical( $queried_object->post_type ) ) { 337 foreach ( (array) get_object_taxonomies( $queried_object->post_type ) as $taxonomy ) { 338 if ( is_taxonomy_hierarchical( $taxonomy ) ) { 339 $term_hierarchy = _get_term_hierarchy( $taxonomy ); 340 $terms = wp_get_object_terms( $queried_object_id, $taxonomy, array( 'fields' => 'ids' ) ); 341 if ( is_array( $terms ) ) { 342 $possible_object_parents = array_merge( $possible_object_parents, $terms ); 343 $term_to_ancestor = array(); 344 foreach ( (array) $term_hierarchy as $ancestor => $descendents ) { 345 foreach ( (array) $descendents as $desc ) { 346 $term_to_ancestor[ $desc ] = $ancestor; 347 } 348 } 349 350 foreach ( $terms as $desc ) { 351 do { 352 $possible_taxonomy_ancestors[ $taxonomy ][] = $desc; 353 if ( isset( $term_to_ancestor[ $desc ] ) ) { 354 $_desc = $term_to_ancestor[ $desc ]; 355 unset( $term_to_ancestor[ $desc ] ); 356 $desc = $_desc; 357 } else { 358 $desc = 0; 359 } 360 } while ( ! empty( $desc ) ); 361 } 362 } 363 } 364 } 365 } elseif ( ! empty( $queried_object->taxonomy ) && is_taxonomy_hierarchical( $queried_object->taxonomy ) ) { 366 $term_hierarchy = _get_term_hierarchy( $queried_object->taxonomy ); 367 $term_to_ancestor = array(); 368 foreach ( (array) $term_hierarchy as $ancestor => $descendents ) { 369 foreach ( (array) $descendents as $desc ) { 370 $term_to_ancestor[ $desc ] = $ancestor; 371 } 372 } 373 $desc = $queried_object->term_id; 374 do { 375 $possible_taxonomy_ancestors[ $queried_object->taxonomy ][] = $desc; 376 if ( isset( $term_to_ancestor[ $desc ] ) ) { 377 $_desc = $term_to_ancestor[ $desc ]; 378 unset( $term_to_ancestor[ $desc ] ); 379 $desc = $_desc; 380 } else { 381 $desc = 0; 382 } 383 } while ( ! empty( $desc ) ); 384 } 385 386 $possible_object_parents = array_filter( $possible_object_parents ); 387 388 $front_page_url = home_url(); 389 $front_page_id = (int) get_option( 'page_on_front' ); 390 $privacy_policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' ); 391 392 foreach ( (array) $menu_items as $key => $menu_item ) { 393 394 $menu_items[ $key ]->current = false; 395 396 $classes = (array) $menu_item->classes; 397 $classes[] = 'menu-item'; 398 $classes[] = 'menu-item-type-' . $menu_item->type; 399 $classes[] = 'menu-item-object-' . $menu_item->object; 400 401 // This menu item is set as the 'Front Page'. 402 if ( 'post_type' === $menu_item->type && $front_page_id === (int) $menu_item->object_id ) { 403 $classes[] = 'menu-item-home'; 404 } 405 406 // This menu item is set as the 'Privacy Policy Page'. 407 if ( 'post_type' === $menu_item->type && $privacy_policy_page_id === (int) $menu_item->object_id ) { 408 $classes[] = 'menu-item-privacy-policy'; 409 } 410 411 // If the menu item corresponds to a taxonomy term for the currently queried non-hierarchical post object. 412 if ( $wp_query->is_singular && 'taxonomy' === $menu_item->type 413 && in_array( (int) $menu_item->object_id, $possible_object_parents, true ) 414 ) { 415 $active_parent_object_ids[] = (int) $menu_item->object_id; 416 $active_parent_item_ids[] = (int) $menu_item->db_id; 417 $active_object = $queried_object->post_type; 418 419 // If the menu item corresponds to the currently queried post or taxonomy object. 420 } elseif ( 421 (int) $menu_item->object_id === $queried_object_id 422 && ( 423 ( ! empty( $home_page_id ) && 'post_type' === $menu_item->type 424 && $wp_query->is_home && $home_page_id === (int) $menu_item->object_id ) 425 || ( 'post_type' === $menu_item->type && $wp_query->is_singular ) 426 || ( 'taxonomy' === $menu_item->type 427 && ( $wp_query->is_category || $wp_query->is_tag || $wp_query->is_tax ) 428 && $queried_object->taxonomy === $menu_item->object ) 429 ) 430 ) { 431 $classes[] = 'current-menu-item'; 432 $menu_items[ $key ]->current = true; 433 $ancestor_id = (int) $menu_item->db_id; 434 435 while ( 436 ( $ancestor_id = (int) get_post_meta( $ancestor_id, '_menu_item_menu_item_parent', true ) ) 437 && ! in_array( $ancestor_id, $active_ancestor_item_ids, true ) 438 ) { 439 $active_ancestor_item_ids[] = $ancestor_id; 440 } 441 442 if ( 'post_type' === $menu_item->type && 'page' === $menu_item->object ) { 443 // Back compat classes for pages to match wp_page_menu(). 444 $classes[] = 'page_item'; 445 $classes[] = 'page-item-' . $menu_item->object_id; 446 $classes[] = 'current_page_item'; 447 } 448 449 $active_parent_item_ids[] = (int) $menu_item->menu_item_parent; 450 $active_parent_object_ids[] = (int) $menu_item->post_parent; 451 $active_object = $menu_item->object; 452 453 // If the menu item corresponds to the currently queried post type archive. 454 } elseif ( 455 'post_type_archive' === $menu_item->type 456 && is_post_type_archive( array( $menu_item->object ) ) 457 ) { 458 $classes[] = 'current-menu-item'; 459 $menu_items[ $key ]->current = true; 460 $ancestor_id = (int) $menu_item->db_id; 461 462 while ( 463 ( $ancestor_id = (int) get_post_meta( $ancestor_id, '_menu_item_menu_item_parent', true ) ) 464 && ! in_array( $ancestor_id, $active_ancestor_item_ids, true ) 465 ) { 466 $active_ancestor_item_ids[] = $ancestor_id; 467 } 468 469 $active_parent_item_ids[] = (int) $menu_item->menu_item_parent; 470 471 // If the menu item corresponds to the currently requested URL. 472 } elseif ( 'custom' === $menu_item->object && isset( $_SERVER['HTTP_HOST'] ) ) { 473 $_root_relative_current = untrailingslashit( $_SERVER['REQUEST_URI'] ); 474 475 // If it's the customize page then it will strip the query var off the URL before entering the comparison block. 476 if ( is_customize_preview() ) { 477 $_root_relative_current = strtok( untrailingslashit( $_SERVER['REQUEST_URI'] ), '?' ); 478 } 479 480 $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_root_relative_current ); 481 $raw_item_url = strpos( $menu_item->url, '#' ) ? substr( $menu_item->url, 0, strpos( $menu_item->url, '#' ) ) : $menu_item->url; 482 $item_url = set_url_scheme( untrailingslashit( $raw_item_url ) ); 483 $_indexless_current = untrailingslashit( preg_replace( '/' . preg_quote( $wp_rewrite->index, '/' ) . '$/', '', $current_url ) ); 484 485 $matches = array( 486 $current_url, 487 urldecode( $current_url ), 488 $_indexless_current, 489 urldecode( $_indexless_current ), 490 $_root_relative_current, 491 urldecode( $_root_relative_current ), 492 ); 493 494 if ( $raw_item_url && in_array( $item_url, $matches, true ) ) { 495 $classes[] = 'current-menu-item'; 496 $menu_items[ $key ]->current = true; 497 $ancestor_id = (int) $menu_item->db_id; 498 499 while ( 500 ( $ancestor_id = (int) get_post_meta( $ancestor_id, '_menu_item_menu_item_parent', true ) ) 501 && ! in_array( $ancestor_id, $active_ancestor_item_ids, true ) 502 ) { 503 $active_ancestor_item_ids[] = $ancestor_id; 504 } 505 506 if ( in_array( home_url(), array( untrailingslashit( $current_url ), untrailingslashit( $_indexless_current ) ), true ) ) { 507 // Back compat for home link to match wp_page_menu(). 508 $classes[] = 'current_page_item'; 509 } 510 $active_parent_item_ids[] = (int) $menu_item->menu_item_parent; 511 $active_parent_object_ids[] = (int) $menu_item->post_parent; 512 $active_object = $menu_item->object; 513 514 // Give front page item the 'current-menu-item' class when extra query arguments are involved. 515 } elseif ( $item_url === $front_page_url && is_front_page() ) { 516 $classes[] = 'current-menu-item'; 517 } 518 519 if ( untrailingslashit( $item_url ) === home_url() ) { 520 $classes[] = 'menu-item-home'; 521 } 522 } 523 524 // Back-compat with wp_page_menu(): add "current_page_parent" to static home page link for any non-page query. 525 if ( ! empty( $home_page_id ) && 'post_type' === $menu_item->type 526 && empty( $wp_query->is_page ) && $home_page_id === (int) $menu_item->object_id 527 ) { 528 $classes[] = 'current_page_parent'; 529 } 530 531 $menu_items[ $key ]->classes = array_unique( $classes ); 532 } 533 $active_ancestor_item_ids = array_filter( array_unique( $active_ancestor_item_ids ) ); 534 $active_parent_item_ids = array_filter( array_unique( $active_parent_item_ids ) ); 535 $active_parent_object_ids = array_filter( array_unique( $active_parent_object_ids ) ); 536 537 // Set parent's class. 538 foreach ( (array) $menu_items as $key => $parent_item ) { 539 $classes = (array) $parent_item->classes; 540 $menu_items[ $key ]->current_item_ancestor = false; 541 $menu_items[ $key ]->current_item_parent = false; 542 543 if ( 544 isset( $parent_item->type ) 545 && ( 546 // Ancestral post object. 547 ( 548 'post_type' === $parent_item->type 549 && ! empty( $queried_object->post_type ) 550 && is_post_type_hierarchical( $queried_object->post_type ) 551 && in_array( (int) $parent_item->object_id, $queried_object->ancestors, true ) 552 && (int) $parent_item->object_id !== $queried_object->ID 553 ) || 554 555 // Ancestral term. 556 ( 557 'taxonomy' === $parent_item->type 558 && isset( $possible_taxonomy_ancestors[ $parent_item->object ] ) 559 && in_array( (int) $parent_item->object_id, $possible_taxonomy_ancestors[ $parent_item->object ], true ) 560 && ( 561 ! isset( $queried_object->term_id ) || 562 (int) $parent_item->object_id !== $queried_object->term_id 563 ) 564 ) 565 ) 566 ) { 567 if ( ! empty( $queried_object->taxonomy ) ) { 568 $classes[] = 'current-' . $queried_object->taxonomy . '-ancestor'; 569 } else { 570 $classes[] = 'current-' . $queried_object->post_type . '-ancestor'; 571 } 572 } 573 574 if ( in_array( (int) $parent_item->db_id, $active_ancestor_item_ids, true ) ) { 575 $classes[] = 'current-menu-ancestor'; 576 577 $menu_items[ $key ]->current_item_ancestor = true; 578 } 579 if ( in_array( (int) $parent_item->db_id, $active_parent_item_ids, true ) ) { 580 $classes[] = 'current-menu-parent'; 581 582 $menu_items[ $key ]->current_item_parent = true; 583 } 584 if ( in_array( (int) $parent_item->object_id, $active_parent_object_ids, true ) ) { 585 $classes[] = 'current-' . $active_object . '-parent'; 586 } 587 588 if ( 'post_type' === $parent_item->type && 'page' === $parent_item->object ) { 589 // Back compat classes for pages to match wp_page_menu(). 590 if ( in_array( 'current-menu-parent', $classes, true ) ) { 591 $classes[] = 'current_page_parent'; 592 } 593 if ( in_array( 'current-menu-ancestor', $classes, true ) ) { 594 $classes[] = 'current_page_ancestor'; 595 } 596 } 597 598 $menu_items[ $key ]->classes = array_unique( $classes ); 599 } 600 } 601 602 /** 603 * Retrieves the HTML list content for nav menu items. 604 * 605 * @uses Walker_Nav_Menu to create HTML list content. 606 * @since 3.0.0 607 * 608 * @param array $items The menu items, sorted by each menu item's menu order. 609 * @param int $depth Depth of the item in reference to parents. 610 * @param stdClass $args An object containing wp_nav_menu() arguments. 611 * @return string The HTML list content for the menu items. 612 */ 613 function walk_nav_menu_tree( $items, $depth, $args ) { 614 $walker = ( empty( $args->walker ) ) ? new Walker_Nav_Menu() : $args->walker; 615 616 return $walker->walk( $items, $depth, $args ); 617 } 618 619 /** 620 * Prevents a menu item ID from being used more than once. 621 * 622 * @since 3.0.1 623 * @access private 624 * 625 * @param string $id 626 * @param object $item 627 * @return string 628 */ 629 function _nav_menu_item_id_use_once( $id, $item ) { 630 static $_used_ids = array(); 631 632 if ( in_array( $item->ID, $_used_ids, true ) ) { 633 return ''; 634 } 635 636 $_used_ids[] = $item->ID; 637 638 return $id; 639 } 640 641 /** 642 * Remove the `menu-item-has-children` class from bottom level menu items. 643 * 644 * This runs on the {@see 'nav_menu_css_class'} filter. The $args and $depth 645 * parameters were added after the filter was originally introduced in 646 * WordPress 3.0.0 so this needs to allow for cases in which the filter is 647 * called without them. 648 * 649 * @see https://core.trac.wordpress.org/ticket/56926 650 * 651 * @since 6.2.0 652 * 653 * @param string[] $classes Array of the CSS classes that are applied to the menu item's `<li>` element. 654 * @param WP_Post $menu_item The current menu item object. 655 * @param stdClass|false $args An object of wp_nav_menu() arguments. Default false ($args unspecified when filter is called). 656 * @param int|false $depth Depth of menu item. Default false ($depth unspecified when filter is called). 657 * @return string[] Modified nav menu classes. 658 */ 659 function wp_nav_menu_remove_menu_item_has_children_class( $classes, $menu_item, $args = false, $depth = false ) { 660 /* 661 * Account for the filter being called without the $args or $depth parameters. 662 * 663 * This occurs when a theme uses a custom walker calling the `nav_menu_css_class` 664 * filter using the legacy formats prior to the introduction of the $args and 665 * $depth parameters. 666 * 667 * As both of these parameters are required for this function to determine 668 * both the current and maximum depth of the menu tree, the function does not 669 * attempt to remove the `menu-item-has-children` class if these parameters 670 * are not set. 671 */ 672 if ( false === $depth || false === $args ) { 673 return $classes; 674 } 675 676 // Max-depth is 1-based. 677 $max_depth = isset( $args->depth ) ? (int) $args->depth : 0; 678 // Depth is 0-based so needs to be increased by one. 679 $depth = $depth + 1; 680 681 // Complete menu tree is displayed. 682 if ( 0 === $max_depth ) { 683 return $classes; 684 } 685 686 /* 687 * Remove the `menu-item-has-children` class from bottom level menu items. 688 * -1 is used to display all menu items in one level so the class should 689 * be removed from all menu items. 690 */ 691 if ( -1 === $max_depth || $depth >= $max_depth ) { 692 $classes = array_diff( $classes, array( 'menu-item-has-children' ) ); 693 } 694 695 return $classes; 696 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Tue Dec 24 08:20:01 2024 | Cross-referenced by PHPXref |