滤器nav_menu_link_attributes
:
add_filter( \'nav_menu_link_attributes\', \'wpse_100726_extra_atts\', 10, 3 );
function wpse_100726_extra_atts( $atts, $item, $args )
{
// inspect $item, then …
$atts[\'custom\'] = \'some value\';
return $atts;
}
这适用于WordPress<;3.6:
add_filter( \'walker_nav_menu_start_el\', function( $item ) {
$parts = explode( \'>\', $item );
$out = array ();
foreach ( $parts as $i => $part )
{
if ( 0 === strpos( $part, \'<a \' ) ) // a start
$out[ $i ] = $part . \' data-foo="bar"\';
else
$out[ $i ] = $part;
}
return join( \'>\', $out );
});