正在将数据属性添加到list item 在菜单中,我可以看到这里的一些线程可以将其添加到实际<a>
链接标记,我希望将其添加到列表项本身<li>
因此,此代码将属性添加到<a>
标签
add_filter( \'nav_menu_link_attributes\', \'themeprefix_menu_attribute_add\', 10, 3 );
function themeprefix_menu_attribute_add( $atts, $item, $args ) {
// Set the menu ID
$menu_link1 = 20;
// Conditionally match the ID and add the attribute and value
if ($item->ID == $menu_link1) {
$atts[\'data-content\'] = \'about\';
//Return the new attribute
return $atts;
如何将属性添加到<li>
不使用jQuery。