创建一个Wordpress自定义沃克

时间:2014-04-16 20:56:33

标签: php wordpress menu

我正在尝试为wordpress网站编写自定义助行器。我不知道任何php编程

这是我拥有的HTML:

<ul id="menu-main-menu-top-1" class="footer-links clearfix">
<div>
<li id="menu-item-65" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-5 current_page_item menu-item-has-children active">
</div>
<div>
<li id="menu-item-73" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children">
</div>
<div>
<li id="menu-item-76" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children">
</div>
<div>
<li id="menu-item-80" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children">
</div>
<div>
<li id="menu-item-84" class="menu-item menu-item-type-post_type menu-item-object-page">
</div>
<div>
<li id="menu-item-85" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children">
</div>
<div>
<li id="menu-item-89" class="menu-item menu-item-type-post_type menu-item-object-page">
</div>
<div>
<li id="menu-item-91" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children">
</div>
</ul>

我希望它是这样的(我想将父级列表项目包含在3个块中的div中)

<ul id="menu-main-menu-top" class="nav navbar-nav">

<div class="wrap">
<li id="menu-item-65" class="dropdown menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-5 current_page_item menu-item-has-children active">
<li id="menu-item-73" class="dropdown menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children">
<li id="menu-item-76" class="dropdown menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children">
</div>

<div class="wrap">
<li id="menu-item-80" class="dropdown menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children">
<li id="menu-item-84" class="menu-item menu-item-type-post_type menu-item-object-page">
<li id="menu-item-85" class="dropdown menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children">
</div>

<div class="wrap">
<li id="menu-item-89" class="menu-item menu-item-type-post_type menu-item-object-page">
<li id="menu-item-91" class="dropdown menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children">
</div>

</ul>

以下是我正在使用的助行器代码:

class footer_links_walker extends Walker_Nav_Menu
{  

  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;

       $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
       $class_names = ' class="'. esc_attr( $class_names ) . '"';


       if ($depth == 0 && $liitem = 1) {$output .= "\n<div>\n";}
       $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 );
        }

        function end_el(&$output, $item, $depth, $args) {
          if ($depth == 0 && $liitem = 3) {$output .= "</div>\n";}
        }

}

function somen_main_nav() {
    // display the wp3 menu if available
    wp_nav_menu( 
        array( 
            'menu' => 'footer_links', /* menu name */
            'menu_class' => 'footer-links clearfix',
            'theme_location' => 'footer_links', /* where in the theme it's assigned */
            'container' => 'false', /* container class */
            'fallback_cb' => 'wp_bootstrap_footer_links_fallback', /* menu fallback */
            // 'depth' => '2',  suppress lower levels for now 
            'walker' => new footer_links_walker()
        )
    );

}

这是一个高级水平的事情要完成,专家将能够帮助我。任何帮助都非常感谢。

由于

1 个答案:

答案 0 :(得分:0)

试试这个:

$walker = new Grouped_Walker_Nav_Menu();

wp_nav_menu(array(
  'menu'       => 'primary',
  'walker'     => $walker,
  'depth'      => 0,
  'container'  => false,
  'items_wrap' => '<ul id="%1$s" class="%2$s">'.$walker->groups_open."\n".'%3$s' //</ul>'
));

$walker->finish();



class Grouped_Walker_Nav_Menu extends Walker_Nav_Menu {  
  private $counters = array(0,0,0,0,0,0);
  private $pergroup = array(3,3,3,3,3,3);
  public  $groups_open  = '<li class="menu-groups">';
  private $groups_close = '</li>';
  private $group_open  = '<ul class="menu-group">';
  private $group_close = '</ul>';

  function start_lvl( &$output, $depth = 0, $args = array() ) {
    $indent = str_repeat("\t", ($depth + 1 ) * 2 );
    $output .= "\n$indent<ul class=\"sub-menu\">{$this->groups_open}\n";
    $this->counters[ $depth ] = 0;
  }

  function end_lvl( &$output, $depth = 0, $args = array() ) {
    $indent = str_repeat("\t", ($depth + 1 ) * 2 );

    // close unclosed
    ++ $depth;
    if( $this->pergroup[ $depth ])
      if( $this->counters[ $depth ] != 0 )
        $output .= "$indent\t{$this->group_close}\n";

    $output .= "$indent{$this->groups_close}</ul>";
  }

  function end_el( &$output, $item, $depth = 0, $args = array() ) {
    $output .= "</li>\n";
    if( $this->pergroup[ $depth ]){
      $indent = str_repeat("\t", $depth * 2 + 1 );
      if( $this->counters[ $depth ] == 0 ) $output .= "$indent{$this->group_close}\n";
    }
  }

  function finish(){
    if( $this->pergroup[0]){
      $indent = str_repeat("\t", 1 );
      if( $this->counters[ 0 ] != 0 ) echo "$indent{$this->group_close}\n";
    }
    echo "{$this->groups_close}</ul>";
  }

  function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
    $indent = str_repeat("\t", $depth * 2 + 1 );

    $class_names = '';

    $classes = empty( $item->classes ) ? array() : (array) $item->classes;
    $classes[] = 'menu-item-' . $item->ID;

    $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
    $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';

    $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
    $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';

    if( $this->pergroup[ $depth ]){
      if( $this->counters[ $depth ] == 0 ) $output .= "$indent{$this->group_open}\n";
      $this->counters[ $depth ] = ( $this->counters[ $depth ] + 1 )  %  $this->pergroup[ $depth ];
    }

    $output .= "$indent\t<li" . $id . $class_names .'>';

    $atts = array();
    $atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
    $atts['target'] = ! empty( $item->target )     ? $item->target     : '';
    $atts['rel']    = ! empty( $item->xfn )        ? $item->xfn        : '';
    $atts['href']   = ! empty( $item->url )        ? $item->url        : '';

    $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );

    $attributes = '';
    foreach ( $atts as $attr => $value ) {
      if ( ! empty( $value ) ) {
        $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
        $attributes .= ' ' . $attr . '="' . $value . '"';
      }
    }

    $item_output = $args->before;
    $item_output .= '<a'. $attributes .'>';
    $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
    $item_output .= '</a>';
    $item_output .= $args->after;

    $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  }

}