我正在尝试为页面和帖子在wp\\u nav\\u菜单中添加一个类来锚定children元素的链接。是否有办法修改导航菜单漫游器并执行此操作?
基本上,我的定制助行器如下所示:
class Main_Nav extends Walker_Nav_Menu {
function start_lvl(&$output, $depth) {
$indent = str_repeat("\\t", $depth);
$output .= "\\n$indent<ul class=\\"dropdown-menu\\">\\n";
}
function start_el(&$output, $item, $depth, $args) {
if( $depth == 0) {
$output .= "<li class=\'menu\'>";
}
if( $depth == 1) {
$output .= "<li>";
}
$attributes = \'\';
! empty( $item->attr_title ) and $attributes .= \' title="\' . esc_attr( $item->attr_title ) .\'"\';
! empty( $item->target )and $attributes .= \' target="\' . esc_attr( $item->target ) .\'"\';
! empty( $item->xfn )and $attributes .= \' rel="\' . esc_attr( $item->xfn ) .\'"\';
! empty( $item->url )and $attributes .= \' href="\' . esc_attr( $item->url ) .\'"\';
$title = apply_filters( \'the_title\', $item->title, $item->ID );
if (/* THE MENU ELEMENT HAS CHILDREN */) {
$item_output =
"<a $attributes class=\'menu\'>"
. $title
. \'</a> \';
}
else {
$item_output =
"<a $attributes>"
. $title
. \'</a> \';
}
$output .= apply_filters(\'walker_nav_menu_start_el\', $item_output, $item, $depth);
}
}
正如你所见,我不知道什么是合适的条件
/* THE ELEMENT HAS CHILDREN */
.
非常感谢。
UPDATE
多亏了泰米尔语,我将代码更新如下:
class Main_Nav extends Walker_Nav_Menu {
function start_lvl(&$output, $depth) {
$indent = str_repeat("\\t", $depth);
$output .= "\\n$indent<ul class=\\"dropdown-menu\\">\\n";
}
function start_el(&$output, $item, $depth, $args) {
if( $depth == 0) {
$output .= "<li class=\'menu\'>";
}
if( $depth == 1) {
$output .= "<li>";
}
$attributes = \'\';
! empty( $item->attr_title ) and $attributes .= \' title="\' . esc_attr( $item->attr_title ) .\'"\';
! empty( $item->target )and $attributes .= \' target="\' . esc_attr( $item->target ) .\'"\';
! empty( $item->xfn )and $attributes .= \' rel="\' . esc_attr( $item->xfn ) .\'"\';
! empty( $item->url )and $attributes .= \' href="\' . esc_attr( $item->url ) .\'"\';
$title = apply_filters( \'the_title\', $item->title, $item->ID );
global $wpdb;
$query = $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status =\'publish\' AND post_type=\'nav_menu_item\' AND post_parent=%d", $item->ID);
$child_count = $wpdb->get_var($query);
if($child_count > 0) { /* THE MENU ELEMENT HAS CHILDREN */
$item_output =
"<a $attributes class=\'menu\'>"
. $title
. \'</a> \';
} else {
$item_output =
"<a $attributes>"
. $title
. \'</a> \';
}
$output .= apply_filters(\'walker_nav_menu_start_el\', $item_output, $item, $depth);
}
}
但运气不好:(
最合适的回答,由SO网友:Dave Romsey 整理而成
通过以下回答,我能够将“父”CSS类添加到有子菜单项的锚定标记中:Add 'has_children' class to parent li when modifying Walker_Nav_Menu
下面是一个示例:
class Main_Nav extends Walker_Nav_Menu {
/**
* @see Walker::start_el()
* @since 3.0.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param int $current_page Menu item ID.
* @param object $args
*/
function start_el(&$output, $item, $depth, $args) {
global $wp_query;
$indent = ( $depth ) ? str_repeat( "\\t", $depth ) : \'\';
$class_names = $value = \'\';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = \'menu-item-\' . $item->ID;
$class_names = join( \' \', apply_filters( \'nav_menu_css_class\', array_filter( $classes ), $item, $args ) );
$class_names = \' class="\' . esc_attr( $class_names ) . \'"\';
$id = apply_filters( \'nav_menu_item_id\', \'menu-item-\'. $item->ID, $item, $args );
$id = strlen( $id ) ? \' id="\' . esc_attr( $id ) . \'"\' : \'\';
$output .= $indent . \'<li\' . $id . $value . $class_names .\'>\';
$attributes = ! empty( $item->attr_title ) ? \' title="\' . esc_attr( $item->attr_title ) .\'"\' : \'\';
$attributes .= ! empty( $item->target ) ? \' target="\' . esc_attr( $item->target ) .\'"\' : \'\';
$attributes .= ! empty( $item->xfn ) ? \' rel="\' . esc_attr( $item->xfn ) .\'"\' : \'\';
$attributes .= ! empty( $item->url ) ? \' href="\' . esc_attr( $item->url ) .\'"\' : \'\';
// Check our custom has_children property.
if ( $args->has_children ) {
$attributes .= \' class="menu parent"\';
}
$item_output = $args->before;
$item_output .= \'<a\'. $attributes .\'>\';
$item_output .= $args->link_before . apply_filters( \'the_title\', $item->title, $item->ID ) . $args->link_after;
$item_output .= \'</a>\';
$item_output .= $args->after;
$output .= apply_filters( \'walker_nav_menu_start_el\', $item_output, $item, $depth, $args );
}
function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {
$id_field = $this->db_fields[\'id\'];
if ( is_object( $args[0] ) ) {
$args[0]->has_children = ! empty( $children_elements[$element->$id_field] );
}
return parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
}
}