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