有没有想通过一些不错的WP论坛上的chaps。
需要创建一个助行器,这是一个丘斯托导航结构:
// nav menu walker
class My_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) . '"';
$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 ) .'"' : '';
$attributes .= ' data-id="'. esc_attr($item->object_id ) .'"';
$attributes .= ' data-slug="'. esc_attr( basename(get_permalink($item->object_id))) .'"';
$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>'; /* This is where I changed things. */
$item_output .= $args->after;
$output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
}
}
然后,只需通过学步车到我的菜单时,我打电话wp_nav_menu();
<?php
$walker = new My_Walker;
wp_nav_menu(array(
'echo' => true,
'menu' => 4,
'container' => '',
'theme_location' => 'primary',
'menu_class' => 'grid-10 omega',
'walker' => $walker
)); ?>
感谢您的分享!正是我需要的! – Norris 2014-06-09 10:59:57
干杯。我必须将签名更改为'function start_el(&$ output,$ item,$ depth = 0,$ args = array(),$ id = 0)'以摆脱错误。 – 2014-10-16 07:06:14