Title: Adding class or id to wp_nav_menu using php
Last modified: August 20, 2016

---

# Adding class or id to wp_nav_menu using php

 *  [paesano2000](https://wordpress.org/support/users/paesano2000/)
 * (@paesano2000)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/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="http://www.mywebsite.com/about/">About</a>
       <a href="http://www.mywebsites.com/products/">Products</a>
       <a href="http://www.mywebsite.com/gallery/">Gallery</a>
       <a href="http://www.mywebsite.com/contact/">Contact</a>
       ```
   
 * And this is the end result I am looking for…
 *     ```
       <a id="nav" class="something-classy" href="http://www.mywebsite.com/about/">About</a>
       <a id="nav" class="something-classy" href="http://www.mywebsites.com/products/">Products</a>
       <a id="nav" class="something-classy" href="http://www.mywebsite.com/gallery/">Gallery</a>
       <a id="nav" class="something-classy" href="http://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)

 *  Thread Starter [paesano2000](https://wordpress.org/support/users/paesano2000/)
 * (@paesano2000)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/adding-class-or-id-to-wp_nav_menu-using-php/#post-2196170)
 * Anyone?
 * _[[please don’t bump](http://codex.wordpress.org/Forum_Welcome#No_Bumping)]_
 *  Thread Starter [paesano2000](https://wordpress.org/support/users/paesano2000/)
 * (@paesano2000)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/adding-class-or-id-to-wp_nav_menu-using-php/#post-2196430)
 * Well, to not make this forum useless, I thought I’d post solution I used to my
   problem. Using the preg_replace php function I successfully added a class to 
   the link.
 * Add this to your functions.php file to add a class to your link navigation.
 *     ```
       function add_nav_class($output) {
           $output= preg_replace('/<a/', '<a class="your-class"', $output, 1);
           return $output;
       }
       add_filter('wp_nav_menu', 'add_nav_class');
       ```
   
 * You can find [more information on the preg_replace php function here.](http://php.net/manual/en/function.preg-replace.php)
 *  [Danny Albeck](https://wordpress.org/support/users/dalbeck/)
 * (@dalbeck)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/adding-class-or-id-to-wp_nav_menu-using-php/#post-2196441)
 * Paesano,
 * I just stumbled on this and found it super useful! I had one question that maybe
   you could help me with. Is there a way to auto generate a # after each class 
   name. Example:
 *     ```
       <a class="nav1">link</a>
       <a class="nav2">link</a>
       ```
   
 * etc.
 * Wasn’t sure if you knew anyway to dynamically generate this with another PHP 
   command on this tag in the functions.php file.
 *  [Danny Albeck](https://wordpress.org/support/users/dalbeck/)
 * (@dalbeck)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/adding-class-or-id-to-wp_nav_menu-using-php/#post-2196443)
 * Or even a way to pull the page / post id on the anchor tag.
 *  [Danny Albeck](https://wordpress.org/support/users/dalbeck/)
 * (@dalbeck)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/adding-class-or-id-to-wp_nav_menu-using-php/#post-2196446)
 * Anyone? Still not getting anywhere with this.
 *  [yolise](https://wordpress.org/support/users/yolise/)
 * (@yolise)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/adding-class-or-id-to-wp_nav_menu-using-php/#post-2196504)
 * I’d like to find a solution to this as well. Currently this function only adds
   a class to the first nav element.
 *  [edmarno](https://wordpress.org/support/users/edmarno/)
 * (@edmarno)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/adding-class-or-id-to-wp_nav_menu-using-php/#post-2196505)
 * Hi,
 * The way you do it is by changing the number after $output to the number of times
   you want the function to happen (-1 for unlimited); so if you want the function
   to do it for every menu item it looks like this:
 * > function add_nav_class($output) {
   >  $output= preg_replace(‘/<a/’, ‘<a class=”
   > your-class”‘, $output, -1); return $output; } add_filter(‘wp_nav_menu’, ‘add_nav_class’);
 *  [ishakali](https://wordpress.org/support/users/ishakali/)
 * (@ishakali)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/adding-class-or-id-to-wp_nav_menu-using-php/#post-2196508)
 * hi edmarno
 *  your function to happen (-1 for unlimited) is really helped me thanks a lot…
   thanks once again
 * Ishak

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.

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)
 * [preg_replace](https://wordpress.org/support/topic-tag/preg_replace/)
 * [wp_nav_menu()](https://wordpress.org/support/topic-tag/wp_nav_menu/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 8 replies
 * 5 participants
 * Last reply from: [ishakali](https://wordpress.org/support/users/ishakali/)
 * Last activity: [14 years, 3 months ago](https://wordpress.org/support/topic/adding-class-or-id-to-wp_nav_menu-using-php/#post-2196508)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
