Adding class or id to wp_nav_menu using php
-
Hello! I am working on a website where I have had to hack the wp_nav_menu a bit striping out all containers…
This is all beacause I am applying some jquery effects to the nav that do not like the extra containers.
What I need help with is adding a class to the link(a tag)of my wp_nav_menu. I’ve been trying to do it with the following php code in the function file, with no success, most likely due to my lack of experience in php:
// Filtering out some HTML and Code from the Nav function wp_nav_menu_remove_attributes( $menu ){ $patterns = array('/ id=\"(.*)\" class=\"(.*)\"/iU', '/<li>/iU'); return $menu = preg_replace($patterns, '', $menu ); } function add_menuclass($aclass) { return preg_replace('/<a>/', '<a id="nav" class="something-classy">', $aclass, 1); } add_filter( 'wp_nav_menu', 'wp_nav_menu_remove_attributes', 'add_menuclass' );
This is the HTML I have:
<a href="https://www.mywebsite.com/about/">About</a> <a href="https://www.mywebsites.com/products/">Products</a> <a href="https://www.mywebsite.com/gallery/">Gallery</a> <a href="https://www.mywebsite.com/contact/">Contact</a>
And this is the end result I am looking for…
<a id="nav" class="something-classy" href="https://www.mywebsite.com/about/">About</a> <a id="nav" class="something-classy" href="https://www.mywebsites.com/products/">Products</a> <a id="nav" class="something-classy" href="https://www.mywebsite.com/gallery/">Gallery</a> <a id="nav" class="something-classy" href="https://www.mywebsite.com/contact/">Contact</a>
Can someone more experienced pick out what I am doing wrong? Help would be so appreciated… I’ve spent hours trying to figure this out.
Viewing 8 replies - 1 through 8 (of 8 total)
Viewing 8 replies - 1 through 8 (of 8 total)
- The topic ‘Adding class or id to wp_nav_menu using php’ is closed to new replies.