php - Add data attribute to wp_nav_menu -
i have following code:
$nav_menu_args = array('fallback_cb' => '','menu' => 'menu', 'menu_class' => 'menu_class'); $x = wp_nav_menu( apply_filters( 'widget_nav_menu_args', $nav_menu_args, 'menu', $args ) ); $pattern = '#<ul([^>]*)>#i'; $replacement = '<ul$1 data-attr="abc">'; // wrong echo preg_replace( $pattern, $replacement, $x );
i trying add data-attr
ul
altering pattern, , without making changes through walker_nav_menu
.
what want have list this:
<ul class="menu_class" data-attr="abc"> <li><li> <li> <ul> <li></li> </ul> <li> </ul>
but data-attr
on inner ul this.
<ul class="menu_class" data-attr="abc"> <li><li> <li> <ul data-attr="abc"> <li></li> </ul> <li> </ul>
what missing?
you add number of objects want replace takes first ul
.
echo preg_replace( $pattern, $replacement, $x, 1 ); // 1 @ end replace first occurence
or change items_wrap key of wp_nav_menu.
$nav_menu_args = array('fallback_cb' => '','menu' => 'menu', 'items_wrap' => '<ul class="menu_class" data-attr="abc">%3$s</ul>');
Comments
Post a Comment