0

任何人都知道如何为 wp_nav_menu 编写一个自定义的 walker 函数,它将......

1) 仅显示父项目的项目描述。

2)仅当它有子项时,才将父项(标题、链接和描述)包装在它自己的 div中。

(我已将 div 和 ul 简化为没有类)

<div>
  <ul>

    <!-- Parent With Children -->
    <li class="has_children">
      <div class="first-level">
        <a>Parent Item</a>
        <span class="desc">Description</span>
      </div>
        <ul class="sub-menu level-0">
          <li class="odd"><a>Child Item</a></li>
          <li class="even NO HAS_CHILDREN"><a>Child Item</a>
            <ul class="sub-menu level-1">
               <li class="odd"><a>Child Item</a></li>
            </ul>
          </li>
          <li class="odd"><a>Child Item</a></li>
          <li class="even"><a>Child Item</a></li>
          <li class="odd"><a>Child Item</a></li>
        </ul>
    </li>

    <!-- Parent Without Children -->
    <li>
     <div class="first-level">
      <a>Parent Item</a>
      <span class="desc">Description</span>
     </div>
    </li>

  </ul>
</div>

^^ 以上所有的类和 ID 都应该是除了那些已经在 wp_nav_menu 中的类和 ID

4

1 回答 1

0

这是基于这个问题这个问题

    class Description_Walker extends Walker_Nav_Menu
{
    /**
     * Start the element output.
     *
     * @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. May be used for padding.
     * @param  array $args    Additional strings.
     * @return void
     */
     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 ) {
    // depth dependent classes
    $indent = ( $depth > 0  ? str_repeat( "\t", $depth ) : '' ); // code indent
    $display_depth = ( $depth); // because it counts the first submenu as 0
    $classes = array('sub-menu','level-' . $display_depth);
    $class_names = implode( ' ', $classes );

    // build html
    $output .= "\n" . $indent . '<ul class="' . $class_names . '">' . "\n";
}
    function start_el(&$output, $item, $depth, $args)
    {

        $classes     = empty ( $item->classes ) ? array () : (array) $item->classes;

        $class_names = join(
            ' '
        ,   apply_filters(
                'nav_menu_css_class'
            ,   array_filter( $classes ), $item
            )
        );

        if ($args->has_children && $depth == 0){
        ! empty ( $class_names )
            and $class_names = ' class="'. esc_attr( $class_names ) . ' has_children"';
        }else{
         ! empty ( $class_names )
            and $class_names = ' class="'. esc_attr( $class_names ) . '"';  
        }

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

        // insert description for top level elements only
        // you may change this
        $description = ( ! empty ( $item->description ) and 0 == $depth )
            ? '<span class="desc">' . esc_attr( $item->description ) . '</span>' : '';

        $title = apply_filters( 'the_title', $item->title, $item->ID );
if ( $depth == 0 ) {//top level items
            $item_output = $args->before
            ."<div class='first-level'>"
            . "<a $attributes>"
            . $args->link_before
            . $title
            . '</a>'
            . $args->link_after
            . $description
            . '</div>'
            . $args->after;
        }else{//everything else
        $item_output = $args->before
            . "<a $attributes>"
            . $args->link_before
            . $title
            . '</a> '
            . $args->link_after
            . $args->after;
        }
        // Since $output is called by reference we don't need to return anything.
        $output .= apply_filters(
            'walker_nav_menu_start_el'
        ,   $item_output
        ,   $item
        ,   $depth
        ,   $args
        );
    }

}

然后用walker调用它

<?php wp_nav_menu( array('walker' => new Description_Walker )); ?>
于 2013-08-08T16:33:21.337 回答