我有个问题,不知道怎么解决。我需要显示当前活动菜单的下拉菜单。我有以下代码:
Class Description_Walker extends Walker_Nav_Menu {
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 );
}
function start_lvl( &$output , $depth = 0 , $args = array() ) {
$indent = str_repeat( "\t", $depth );
$output .= "\n$indent<ul class=\"v-dropdown \">\n";
}
function start_el(&$output, $item, $depth = 0, $args = array(), $current_object_id = 0)
{
global $wp_query;
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$class_names = $value = '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
$class_names = ' '. esc_attr( $class_names ) . '';
$output .= $indent . '<li >';
$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 ) .'"' : '';
$prepend='';
$append = '';
$description = ! empty( $item->description ) ? '<span>'.esc_attr( $item->description ).'</span>' : '';
$item_output = $args->before;
if ( $args->has_children ) {
$append=" ▾";
$item_output .= '<a class="dropdown" data-hover=" ">';
$item_output .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID ).$append;
$item_output .= $description.$args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;
} else {
$item_output .= '<a class="'.esc_attr( $class_names ).'" '. $attributes .' data-hover=" '.preg_replace(' ', $item->title).$append.'" >';
$item_output .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID ).$append;
$item_output .= $description.$args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;
}
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
}
网站是这样的:site
当子菜单处于活动状态时,我需要显示ul class=“v-下拉菜单”。请帮帮我。
谢谢。
发布于 2015-04-22 07:21:10
我会这么做的。
您可以将您的<header class="relative">
交换为以下内容:
<header class="relative <?php if ( $post->post_parent == '47' ) { ?>show-menu<?php } ?>" >
替换“47”所在的父页ID。如果您使用父id设置了这些页面,则为。
如果没有,您可以这样做,使用相同的<header class="relative">
设置。
<header class="relative <?php if ( is_page('services') || is_page('corporate-advisory') || is_page('audit-and-accounting') || is_page ('international-orientation-for-smi')) { ?>show-menu<?php } ?>" >
然后,只需在css中添加如下css类:
.show-menu ul li:nth-of-type(3) .v-dropdown {display: block; }
我在本地设置了一个测试,我认为这两种方法都可能适用于您。如果我需要澄清的话请告诉我。
编辑:,我在css中添加了一个psuedo类,目标是“.v-下拉列表”的第一个实例,以便只有第一个菜单打开。
编辑2:我更新了答案以反映正确的css,这有助于AnmA解决问题。
https://stackoverflow.com/questions/29799145
复制相似问题