|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* LSX functions and definitions - Navigation Walker. |
|
4
|
|
|
* |
|
5
|
|
|
* @package lsx |
|
6
|
|
|
* @subpackage navigation |
|
7
|
|
|
* @category bootstrap-walker |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
11
|
|
|
exit; |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
|
|
add_filter( 'nav_menu_item_id', '__return_null' ); |
|
15
|
|
|
|
|
16
|
|
|
if ( ! function_exists( 'lsx_nav_menu_css_class' ) ) : |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Remove the id="" on nav menu items. |
|
20
|
|
|
* Return 'menu-slug' for nav menu classes. |
|
21
|
|
|
* |
|
22
|
|
|
* @package lsx |
|
23
|
|
|
* @subpackage navigation |
|
24
|
|
|
* @category bootstrap-walker |
|
25
|
|
|
*/ |
|
26
|
|
|
function lsx_nav_menu_css_class( $classes, $item ) { |
|
27
|
|
|
$slug = sanitize_title( $item->title ); |
|
28
|
|
|
$classes = preg_replace( '/(current(-menu-|[-_]page[-_])(item|parent|ancestor))/', 'active', $classes ); |
|
29
|
|
|
$classes = preg_replace( '/^((menu|page)[-_\w+]+)+/', '', $classes ); |
|
30
|
|
|
|
|
31
|
|
|
$classes[] = 'menu-' . $slug; |
|
32
|
|
|
$classes = array_unique( $classes ); |
|
33
|
|
|
|
|
34
|
|
|
return array_filter( $classes, 'lsx_is_element_empty' ); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
endif; |
|
38
|
|
|
|
|
39
|
|
|
add_filter( 'nav_menu_css_class', 'lsx_nav_menu_css_class', 10, 2 ); |
|
40
|
|
|
|
|
41
|
|
|
if ( ! function_exists( 'lsx_nav_menu_args' ) ) : |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Clean up wp_nav_menu_args. |
|
45
|
|
|
* |
|
46
|
|
|
* Remove the container. |
|
47
|
|
|
* Use LSX_Nav_Walker() by default. |
|
48
|
|
|
* |
|
49
|
|
|
* @package lsx |
|
50
|
|
|
* @subpackage navigation |
|
51
|
|
|
* @category bootstrap-walker |
|
52
|
|
|
*/ |
|
53
|
|
|
function lsx_nav_menu_args( $args = '' ) { |
|
54
|
|
|
$roots_nav_menu_args['container'] = false; |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
if ( ! $args['items_wrap'] ) { |
|
57
|
|
|
$roots_nav_menu_args['items_wrap'] = '<ul class="%2$s">%3$s</ul>'; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if ( current_theme_supports( 'bootstrap-top-navbar' ) && ! $args['depth'] ) { |
|
61
|
|
|
$roots_nav_menu_args['depth'] = 2; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if ( ! $args['walker'] ) { |
|
65
|
|
|
$roots_nav_menu_args['walker'] = new LSX_Nav_Walker(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return array_merge( $args, $roots_nav_menu_args ); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
endif; |
|
72
|
|
|
|
|
73
|
|
|
add_filter( 'wp_nav_menu_args', 'lsx_nav_menu_args' ); |
|
74
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.