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’sfunctions.php
file and you’re set. // Add ID and CLASS attributes to the first <ul> occurence in wp_page_menufunction
add_menuclass(
$ulclass
) { return
preg_replace(
'/<ul>/'
,
'<ul id="nav">'
,
$ulclass
, 1); } add_filter(
'wp_page_menu'
,
'add_menuclass'
);
Filed under: PHP, Wordpress Tagged: PHP, WORDPRESS
