如何将顺序号添加到Wordpress菜单

时间:2017-10-13 15:48:47

标签: php wordpress menu wp-nav-walker

我想在Wordpress菜单中的每个项目前添加数字。

01首页
02关于
03投资组合
04联系人

我知道你必须使用助行器,但我无法让计数器正常工作。

<?php
class my_nav_walker extends Walker {

// Tell Walker where to inherit it's parent and id values
var $db_fields = array(
    'parent' => 'menu_item_parent', 
    'id'     => 'db_id' 
);

function start_lvl( &$output, $depth = 0, $args = array() ) {
  if (!isset($_GLOBALS['menu_counter'])) {
      $GLOBALS['menu_counter'] = array();
  }
  $GLOBALS['menu_counter'][$depth] = 0;
  $indent = str_repeat("\t", $depth);
  $output .= "\n$indent<ul class=\"sub-menu\">\n";
}

/**
 * At the start of each element, output a <li> and <a> tag structure.
 * 
 * Note: Menu objects include url and title properties, so we will use those.
 */
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
  global $menu_counter;
  $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
  $item_output .= ($args->has_children && $depth == 0) ? ' <span class="caret"></span></a>' : '</a>';
  $item_output .= $args->after . '<span class="navnum">' . str_pad(++$menu_order[$depth], 2, "0", STR_PAD_LEFT) . '.</span>';

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

这是我试图与其他人拼凑起来的。

这就是:

Home01 About01 Portfolio01 Contact01

0 个答案:

没有答案