下拉列表中的Wordpress菜单描述

时间:2015-03-06 20:24:46

标签: wordpress

我希望在我的菜单的下拉菜单中显示菜单说明。

我正在使用带有下拉列表的Nav Menu Walker,该下拉菜单支持我当前菜单的Bootstrap,我有一个Nav Menu Walker的示例,只有描述。

我如何将描述放入下拉链接? (链接 - 说明)

以下是带有描述的助行器:   

         $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 = ' class="'. esc_attr( $class_names ) . '"';

         $output .= $indent . '<li id="menu-item-'. $item->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        ) .'"' : '';

         $prepend = '<strong>';
         $append = '</strong>';
         $description  = ! empty( $item->description ) ? '<span>'.esc_attr( $item->description ).'</span>' : '';

         if($depth != 0)
         {
                $description = $append = $prepend = "";
         }

          $item_output = $args->before;
          $item_output .= '<a'. $attributes .'>';
          $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 );
          }

}

这是我现在的步行者:

class wp_bootstrap_navwalker extends Walker_Nav_Menu {

        /* Start of the <ul>
         *
         * Note on $depth: Counterintuitively, $depth here means the "depth right before we start this menu".
         *                 So basically add one to what you'd expect it to be
         */
        function start_lvl(&$output, $depth) {
            $tabs = str_repeat("\t", $depth);
            // If we are about to start the first submenu, we need to give it a dropdown-menu class
            if ($depth == 0 || $depth == 1) { //really, level-1 or level-2, because $depth is misleading here (see note above)
                $output .= "\n{$tabs}<ul class=\"dropdown-menu\">\n";
            } else {
                $output .= "\n{$tabs}<ul>\n";
            }

            return;
        }

        /* End of the <ul>
         *
         * Note on $depth: Counterintuitively, $depth here means the "depth right before we start this menu".
         *                 So basically add one to what you'd expect it to be
         */
        function end_lvl(&$output, $depth) {
            if ($depth == 0) { // This is actually the end of the level-1 submenu ($depth is misleading here too!)

                // we don't have anything special for Bootstrap, so we'll just leave an HTML comment for now
                $output .= '<!--.dropdown-->';
            }
            $tabs = str_repeat("\t", $depth);
            $output .= "\n{$tabs}</ul>\n";

            return;
        }

        /* Output the <li> and the containing <a>
         * Note: $depth is "correct" at this level
         */
        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;

            /* If this item has a dropdown menu, add the 'dropdown' class for Bootstrap */
            if ($item->hasChildren) {
                $classes[] = 'dropdown';
                // level-1 menus also need the 'dropdown-submenu' class
                if ($depth == 1) {
                    $classes[] = 'dropdown-submenu';
                }
            }

            $prepend     = '<strong>';
            $append      = '</strong>';
            $description = !empty($item->description) ? '<span>' . esc_attr($item->description) . '</span>' : '';

            if ($depth != 0) {
                $description = $append = $prepend = "";
            }

            /* This is the stock Wordpress code that builds the <li> with all of its attributes */
            $class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item));
            $class_names = ' class="' . esc_attr($class_names) . '"';
            $output .= $indent . '<li id="menu-item-' . $item->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) . '"' : '';
            $item_output = $args->before;

            /* If this item has a dropdown menu, make clicking on this link toggle it */
            if ($item->hasChildren && $depth == 0) {
                $item_output .= '<a' . $attributes . ' class="dropdown-toggle" data-toggle="dropdown">';
            } else {
                $item_output .= '<a' . $attributes . '>';
            }

            $item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID) . $args->link_after;

            /* Output the actual caret for the user to click on to toggle the menu */
            if ($item->hasChildren && $depth == 0) {
                $item_output .= '<b class="caret"></b></a>';
            } else {
                $item_output .= '</a>';
            }

            $item_output .= $args->after;
            $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);

            return;
        }

        /* Close the <li>
         * Note: the <a> is already closed
         * Note 2: $depth is "correct" at this level
         */
        function end_el(&$output, $item, $depth, $args) {
            $output .= '</li>';

            return;
        }

        /* Add a 'hasChildren' property to the item
         * Code from: http://wordpress.org/support/topic/how-do-i-know-if-a-menu-item-has-children-or-is-a-leaf#post-3139633
         */
        function display_element($element, &$children_elements, $max_depth, $depth = 0, $args, &$output) {
            // check whether this item has children, and set $item->hasChildren accordingly
            $element->hasChildren = isset($children_elements[$element->ID]) && !empty($children_elements[$element->ID]);

            // continue with normal behavior
            return parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
        }
    }

1 个答案:

答案 0 :(得分:1)

目前还不完全清楚您要在何处放置说明,但是,例如,如果您想在链接标题之后使用它,则可以在start_el函数中的Walker中执行以下操作:< / p>

您目前的位置:

/* If this item has a dropdown menu, make clicking on this link toggle it */
if ($item->hasChildren && $depth == 0) {
    $item_output .= '<a' . $attributes . ' class="dropdown-toggle" data-toggle="dropdown">';
} else {
    $item_output .= '<a' . $attributes . '>';
}

$item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID) . $args->link_after;

将最后一行替换为:

$item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID);
$item_output .= ' - <span class="desc">' . $item->description . '</span>';    
$item_output .= $args->link_after;

这会在标题后插入menu_item说明(如title - description)。

如果您只想要子菜单上的说明,请使用以下变体:

$item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID);
if ($depth > 0) {
    $item_output .= ' - <span class="desc">' . $item->description . '</span>';    
}
$item_output .= $args->link_after;