Php Wordpress-维护;主动的;在父菜单项上初始化

Php Wordpress-维护;主动的;在父菜单项上初始化,php,wordpress,menu,Php,Wordpress,Menu,我正在Wordpress中实现一个主题。此主题有一个顶部导航菜单,每个父项下都有水平子菜单。它将“活动”类放在当前已查看的父项上(否则不会显示其子菜单项)。通过在functions.php中使用这两个函数,我设法在当前父项上维护“active”类 /** * Wp Nav Menu Customizer. */ function special_nav_class($classes, $item){ if( in_array('current-menu-item', $classe

我正在Wordpress中实现一个主题。此主题有一个顶部导航菜单,每个父项下都有水平子菜单。它将“活动”类放在当前已查看的父项上(否则不会显示其子菜单项)。通过在functions.php中使用这两个函数,我设法在当前父项上维护“active”类

/**
 * Wp Nav Menu Customizer.
 */
function special_nav_class($classes, $item){
     if( in_array('current-menu-item', $classes) ){
             $classes[] = 'active ';
     }
     return $classes;
}
add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);


class SH_Last_Walker extends Walker_Nav_Menu{

   function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {

        $id_field = $this->db_fields['id'];

       //If the current element has children, add class 'sub-menu'
       if( isset($children_elements[$element->$id_field]) ) { 
            $classes = empty( $element->classes ) ? array() : (array) $element->classes;
            $classes[] = 'has-sub-menu';
            $element->classes =$classes;
       }
        // We don't want to do anything at the 'top level'.
        if( 0 == $depth )
            return parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );

        //Get the siblings of the current element
        $parent_id_field = $this->db_fields['parent'];      
        $parent_id = $element->$parent_id_field;
        $siblings = $children_elements[ $parent_id ] ;

        //No Siblings?? 
        if( ! is_array($siblings) )
            return parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );

        //Get the 'last' of the siblings.
        $last_child = array_pop($siblings);
        $id_field = $this->db_fields['id'];

            //If current element is the last of the siblings, add class 'last'
        if( $element->$id_field == $last_child->$id_field ){
            $classes = empty( $element->classes ) ? array() : (array) $element->classes;
            $classes[] = 'last';
            $element->classes =$classes;
        }

        return parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
    }
}
但现在我的问题是,当我单击任何父菜单项的子菜单或子菜单项时,它会成功地将我带到子页面,但会从父菜单项中删除“活动”类并将其放在子菜单项上(因为它是当前查看的页面)。我不希望它把这个类放到子类中,我希望它在子类被查看时都在那里(在父项上)


任何帮助都将不胜感激。

如果您只想通过针对父元素添加一些样式或其他属性,WordPress将为您提供内置类,例如:


当你在一个子页面上时,你可以看到像“当前”菜单“父级当前页面”“父级当前页面”“父级当前页面”“父级祖先”这样的类,这些类是通过WordPress添加到父级页面的,你可以选择其中任何一个类来定位父级元素。

如果你想在子菜单项打开时激活下拉菜单“父级当前页面”活动,然后您可以检查当前菜单祖先或当前页面祖先。。就你而言,我认为目前的菜单检查是最有利的

function special_nav_class($classes, $item){
     if( in_array('current-menu-item', $classes) || in_array('current-menu-ancestor', $classes) ){
             $classes[] = 'active ';
     }
     return $classes;
}

我希望这会有所帮助,祝您愉快:)

当您单击子菜单项时,WordPress将向该子菜单项添加
。当前菜单项将向该子菜单项添加
。当前菜单父菜单项将向该子菜单项的父菜单项添加
。您可以根据需要设置这些特定类的样式。

如果您只需要更改菜单中父项的样式,您可以使用css或jQuery轻松管理。非常感谢Sovit Tamrakar。你是我的英雄。你让我开心。我爱你,朋友;)很好的信息,但是这些是wordpress的内置类,假设我有自己的类,那么如何告诉wordpress在当前页面的父菜单项或父菜单项上使用我的类。这就是我想知道的:)