Adding Class To The WordPress Page Menu

Posted: March 5, 2011 in PHP, Wordpress
Tags: ,

Problem: You need to add a class or id to the the unordered list in wp_page_menu. Maybe to implement some super-slick drop down page menus. But! That <ul> tag is trapped inside of wp_page_menu. What are you going to do?

Solution: Filter wp_page_menu. In the code example below we’ll use preg_replace to find the first—and only the first—

<ul> tag and swap it out for <ul id="nav">. Just drop this code snippet into your theme’s functions.php file and you’re set.
// Add ID and CLASS attributes to the first <ul> occurence in wp_page_menu

function add_menuclass($ulclass) { return preg_replace('/<ul>/', '<ul id="nav">', $ulclass, 1); } add_filter('wp_page_menu','add_menuclass');



Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s