假设html是由侧边栏中的wordpress菜单输出的,则必须扩展Walker_Nav_Menu Class 菜单模板函数使用wp_nav_menu
. 像stated in the reference of wp_nav_menu, 您可以修改其输出,如下所示:
class My_DataId_Walker_Nav_Menu extends Walker_Nav_Menu {
// other code found in original Walker_Nav_Menu class
// changing output of li elemens
$output .= $indent
. \'<li id="nav-menu-item-\' . $item->ID . \'" \'
. \'data-id="\' . $item->ID . \'" \'
. \'class="\' . $depth_class_names . \' \' . $class_names . \'">\';
// other code found in original Walker_Nav_Menu class
}
// in the template file, output menu with custom walker class
wp_nav_menu(
array(
\'theme_location\' =>\'primary\', // your menu position
\'walker\' => new My_DataId_Walker(),
\'depth\' => 0
)
);