Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- ЭТО МИНИМАЛЬНЫЙ ПРИМЕР!
- MyTheme переименуйте в имя своей темы
- Как подключить:
- 1) сохранить этот файл (например, в папке wp-content/themes/mytheme/includes/MyTheme_Menu_Walker.php
- 2) в functions.php подключить его (require('includes/MyTheme_Menu_Walker.php'))
- 3) при вызове wp_nav_menu, указать параметр 'walker' => new MyTheme_Menu_Walker()
- */
- class MyTheme_Menu_Walker extends Walker_Nav_Menu {
- /*
- Сортировка флагов
- */
- private static function sortFlags($a, $b) {
- return strcmp($a->language, $b->language);
- }
- public function walk($menu, $depth) {
- usort($menu, array('MyTheme_Menu_Walker', 'sortFlags'));
- return parent::walk($menu, -1);
- }
- public function start_lvl( &$output, $depth = 0, $args = array() ) {}
- public function end_lvl() {}
- public function start_el(&$output, $item, $depth, $args = array()) {
- $atts = array();
- $atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
- $atts['target'] = ! empty( $item->target ) ? $item->target : '';
- $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
- $atts['href'] = ! empty( $item->url ) ? $item->url : '';
- $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
- $attributes = '';
- foreach ( $atts as $attr => $value ) {
- if ( ! empty( $value ) ) {
- $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
- $attributes .= ' ' . $attr . '="' . $value . '"';
- }
- }
- $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
- $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
- $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth );
- $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
- $item_output .= '<a ' . $id . $class_names . $attributes . '>';
- if(isset($item->language)) {
- // изображение флага (путь /wp-content/themes/MyTheme/img/flags/[LANGUAGE].png)
- $item_output .= '<img src="' . get_template_directory_uri() . '/img/flags/' . $item->language . '.png">';
- }
- $item_output .= '</a>';
- $item_output .= $args->after;
- $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
- }
- public function end_el(&$output, $item, $depth) {}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement