php - Modify selector to only target a specific tag [wordpress] -
code below target anchor link in wordpress navigation menu have new attribute 'data-foo' value 'bar'. how can modified target menu specific class?
add_filter( 'walker_nav_menu_start_el', function( $item ) { $parts = explode( '>', $item ); $out = array (); foreach ( $parts $i => $part ) { if ( 0 === strpos( $part, '<a ' ) ) // start $out[ $i ] = $part . ' data-foo="bar"'; else $out[ $i ] = $part; } return join( '>', $out ); });
in code "walker_nav_menu_start_el" pre-defined "filter". can use $get_body_class(classname) apply filter elements class.
$class = $get_body_class('yourclassnamehere'); add_filter( $class, function( $item ) { $parts = explode( '>', $item ); $out = array (); foreach ( $parts $i => $part ) { if ( 0 === strpos( $part, '<a ' ) ) // start $out[ $i ] = $part . ' data-foo="bar"'; else $out[ $i ] = $part; } return join( '>', $out ); });
Comments
Post a Comment