Skip to content

Instantly share code, notes, and snippets.

@AntonLitvin
Created January 8, 2021 18:41
Show Gist options
  • Save AntonLitvin/76f75184763bac79d0207284d3bc3abd to your computer and use it in GitHub Desktop.
Save AntonLitvin/76f75184763bac79d0207284d3bc3abd to your computer and use it in GitHub Desktop.
Добавление к последнему по вложенности подменю дополнительного класса
<?php
class example_walker extends Walker_Nav_Menu {
// в этом свойстве сохраняем значение максимального вложения
public $last_sub_menu_depth = 0;
public function start_lvl( &$output, $depth = 0, $args = null ) {
if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
$t = '';
$n = '';
} else {
$t = "\t";
$n = "\n";
}
$indent = str_repeat( $t, $depth );
// Default class.
$classes = array( 'sub-menu' );
// сравниваем текущее значение рекурсии с максимальным
if ( $depth === $this->last_sub_menu_depth ) {
array_push( $classes, 'last-sub-menu' );
}
$class_names = join( ' ', apply_filters( 'nav_menu_submenu_css_class', $classes, $args, $depth ) );
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
$output .= "{$n}{$indent}<ul$class_names>{$n}";
}
function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {
// обнуляем значение для текущего элемента и запускаем метод на подсчет максимального вложения, только когда элемент имеет дочерние элементы
if ( ! empty( $children_elements[ $element->{$this->db_fields['id']} ] ) ) {
$this->last_sub_menu_depth = 0;
$this->get_last_depth( $element, $children_elements, $max_depth, $depth );
}
return parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
}
// метод для рекурсивного прохода по дочерним пунктам меню
public function get_last_depth( $element, $children_elements, $max_depth, $depth ) {
if ( ! $element ) {
return;
}
$id_field = $this->db_fields['id'];
$id = $element->$id_field;
if ( ( 0 == $max_depth || $max_depth > $depth + 1 ) && isset( $children_elements[ $id ] ) ) {
foreach ( $children_elements[ $id ] as $child ) {
$this->last_sub_menu_depth = $depth;
$this->get_last_depth( $child, $children_elements, $max_depth, $depth + 1 );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment