問題描述

我正在使用 wp_nav_menu() 自定義主題。我想做的是在選單項中新增一個插入符號,它們有 sub-menus 。例如,如果我的選單如下所示:

  • 選單項 1

  • 選單項 2

    • 選單項 2a

    • 選單項 2b

  • 選單項 3

我想要這樣格式化:

  • 列表項

  • 選單項 1

  • 選單項 2>

    • 選單項 2a

    • 選單項 2b

  • 選單項 3

不知道選單的結構。這似乎是一個很常見的格式化問題,所以我想知道是否有任何內建功能為此提供。

最佳解決方案

我這樣做使用 jQuery(因為它不一定需要在 TEXT(對於螢幕閱讀器等) – 只是另一個選擇…:

jQuery(document).ready(function() {
  jQuery('ul#nav li').has('ul').addClass('parentul');
});

那麼對於那個”parentul” 類,我把一個箭頭的背景影像放在右邊> …

次佳解決方案

您可以使用自定義步行器來執行此操作。將以下類貼上到 functions.php 的底部:

class Nfr_Menu_Walker extends Walker_Nav_Menu{

                /**
         * Traverse elements to create list from elements.
         *
         * Display one element if the element doesn't have any children otherwise,
         * display the element and its children. Will only traverse up to the max
         * depth and no ignore elements under that depth. It is possible to set the
         * max depth to include all depths, see walk() method.
         *
         * This method shouldn't be called directly, use the walk() method instead.
         *
         * @since 2.5.0
         *
         * @param object $element Data object
         * @param array $children_elements List of elements to continue traversing.
         * @param int $max_depth Max depth to traverse.
         * @param int $depth Depth of current element.
         * @param array $args
         * @param string $output Passed by reference. Used to append additional content.
         * @return null Null on failure with no changes to parameters.
         */
        function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {

                if ( !$element )
                        return;

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

                //display this element
                if ( is_array( $args[0] ) )
                        $args[0]['has_children'] = ! empty( $children_elements[$element->$id_field] );

                //Adds the 'parent' class to the current item if it has children
                if( ! empty( $children_elements[$element->$id_field] ) ) {
                        array_push($element->classes,'parent');
                        $element->title .= ' >';
                }

                $cb_args = array_merge( array(&$output, $element, $depth), $args);

                call_user_func_array(array(&$this, 'start_el'), $cb_args);

                $id = $element->$id_field;

                // descend only when the depth is right and there are childrens for this element
                if ( ($max_depth == 0 || $max_depth > $depth+1 ) && isset( $children_elements[$id]) ) {

                        foreach( $children_elements[ $id ] as $child ){

                                if ( !isset($newlevel) ) {
                                        $newlevel = true;
                                        //start the child delimiter
                                        $cb_args = array_merge( array(&$output, $depth), $args);
                                        call_user_func_array(array(&$this, 'start_lvl'), $cb_args);
                                }
                                $this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output );
                        }
                        unset( $children_elements[ $id ] );
                }

                if ( isset($newlevel) && $newlevel ){
                        //end the child delimiter
                        $cb_args = array_merge( array(&$output, $depth), $args);
                        call_user_func_array(array(&$this, 'end_lvl'), $cb_args);
                }

                //end this element
                $cb_args = array_merge( array(&$output, $element, $depth), $args);
                call_user_func_array(array(&$this, 'end_el'), $cb_args);
        }
}

然後在 header.php(或者你的 wp_nav_menu 是) 做這樣的事情:

<?php
    $walker = new Nfr_Menu_Walker();
    wp_nav_menu( array( 'theme_location' => 'primary', 'walker' => $walker ) );
?>

Walker 的關鍵部分是以下 if 語句:

if( ! empty( $children_elements[$element->$id_field] ) ) {
                    array_push($element->classes,'parent');
                    $element->title .= ' &gt;';
}

這將檢查該項是否有子項,如果該項新增了’parent’ css 類,然後將該標題從’xxxx’ 更改為’xxxx >’ 。

(改編自 http://wordpress.org/support/topic/wp_nav_menu-add-a-parent-class)

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。