The problem. The built-in wp_list_pages() and wp_list_categories() functions allow lots of things, but they do not allow you to embed a <span> element so that you can use the well-known CSS sliding-doors technique. Happily, with some help from PHP and regular expressions, we can use this awesome technique on a WordPress blog.
- Create the images you need, and then edit the style.css file in your WordPress theme. Here is an example:
01#nav a, #nav a:visited {02display:block;03}04#nav a:hover, #nav a:active {05background:url(images/tab-right.jpg)no-repeat100%1px;06float:left;07}08#nav a span {09float:left;10display:block;11}12#nav a:hover span {13float:left;14display:block;15background:url(images/tab-left.jpg)no-repeat01px;16} - Now it is time to edit the header.php file. Simply copy and paste one of the following codes, according to your needs:To list your pages:
<ul id="nav"> <li><a href="<?php echo get_option('home'); ?>/"><span>Home</span></a></li> <?php echo preg_replace('@\<li([^>]*)>\<a([^>]*)>(.*?)\<\/a>@i', '<li$1><a$2><span>$3</span></a>', wp_list_pages('echo=0&orderby=name&exlude=181&title_li=&depth=1')); ?> </ul>To list your categories:
<ul id="nav"> <li><a href="<?php echo get_option('home'); ?>/"><span>Home</span></a></li> <?php echo preg_replace('@\<li([^>]*)>\<a([^>]*)>(.*?)\<\/a>@i', '<li$1><a$2><span>$3</span></a>', wp_list_categories('echo=0&orderby=name&exlude=181&title_li=&depth=1')); ?> </ul>
Code explanation. In this example, we make use of the echo=0 parameter in the wp_list_pages() and wp_list_categories() functions, which allows you to get the result of the function without directly printing it on the screen. Then, the result of the function is used by the PHP preg_replace() function and finally displayed with <span> tags added between the <li> and <a> tags.
