Advertisement
Guest User

MyTheme_Menu_Walker.php

a guest
Nov 20th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.69 KB | None | 0 0
  1. <?php
  2.     /*
  3.         ЭТО МИНИМАЛЬНЫЙ ПРИМЕР!
  4.  
  5.         MyTheme переименуйте в имя своей темы
  6.  
  7.         Как подключить:
  8.         1) сохранить этот файл (например, в папке wp-content/themes/mytheme/includes/MyTheme_Menu_Walker.php
  9.         2) в functions.php подключить его (require('includes/MyTheme_Menu_Walker.php'))
  10.         3) при вызове wp_nav_menu, указать параметр 'walker' => new MyTheme_Menu_Walker()
  11.     */
  12.     class MyTheme_Menu_Walker extends Walker_Nav_Menu {
  13.  
  14.         /*
  15.             Сортировка флагов
  16.         */
  17.         private static function sortFlags($a, $b) {
  18.             return strcmp($a->language, $b->language);
  19.         }
  20.  
  21.         public function walk($menu, $depth) {
  22.             usort($menu, array('MyTheme_Menu_Walker', 'sortFlags'));
  23.             return parent::walk($menu, -1);
  24.         }
  25.  
  26.         public function start_lvl( &$output, $depth = 0, $args = array() ) {}
  27.         public function end_lvl() {}
  28.  
  29.         public function start_el(&$output, $item, $depth, $args = array()) {
  30.            
  31.             $atts = array();
  32.             $atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
  33.             $atts['target'] = ! empty( $item->target )     ? $item->target     : '';
  34.             $atts['rel']    = ! empty( $item->xfn )        ? $item->xfn        : '';
  35.             $atts['href']   = ! empty( $item->url )        ? $item->url        : '';
  36.  
  37.             $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
  38.  
  39.             $attributes = '';
  40.             foreach ( $atts as $attr => $value ) {
  41.                 if ( ! empty( $value ) ) {
  42.                     $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
  43.                     $attributes .= ' ' . $attr . '="' . $value . '"';
  44.                 }
  45.             }
  46.  
  47.             $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
  48.             $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
  49.  
  50.             $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth );
  51.             $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
  52.  
  53.             $item_output .= '<a ' . $id . $class_names . $attributes . '>';
  54.  
  55.             if(isset($item->language)) {
  56.                 // изображение флага (путь /wp-content/themes/MyTheme/img/flags/[LANGUAGE].png)
  57.                 $item_output .= '<img src="' . get_template_directory_uri() . '/img/flags/' . $item->language . '.png">';
  58.             }
  59.  
  60.             $item_output .= '</a>';
  61.             $item_output .= $args->after;
  62.            
  63.             $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  64.         }
  65.  
  66.         public function end_el(&$output, $item, $depth) {}
  67.  
  68.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement