This sliding doors CSS hack allows you to create sophisticated tabs for your navigation bar. Sadly, WordPress core functions wp_list_pages() and wp_list_categories() don’t allow you to add the required span tag to use this technique.
We are going to see how to proceed in order to use sliding doors in our WordPress theme.
Sliding doors, why?
There’s many articles available on the web about the sliding doors technique, so I’m not going to talk a lot about it. For people who don’t already know this famous hack, here’s a quick example.
Let’s build a typical navigation list:
<ul id="nav">
<li><a href="#">link n°1</a></li>
<li><a href="#">link n°2</a></li>
<li><a href="#">link n°3</a></li>
</ul>
If we apply, via CSS, background images to our links in order to make this menu prettier, we’ll quickly see a big problem: We must add a fixed width to the links, otherwise the image will be truncated if the link is too short, or the link will overflow the image if its width is too long.
That’s why sliding doors are very useful: We just have to add a span element inside the link, and then, in our CSS, assign a different background image to both the span element and the link.
<ul id="nav">
<li><a href="#"><span>link n°1</span></a></li>
<li><a href="#"><span>link n°2</span></a></li>
<li><a href="#"><span>link n°3</span></a></li>
</ul>
Our CSS should look like this:
#nav a, #nav a:visited {
display:block;
}
#nav a:hover, #nav a:active {
background:url(images/tab-right.jpg) no-repeat 100% 1px;
float:left;
}
#nav a span {
float:left;
display:block;
}
#nav a:hover span {
float:left;
display:block;
background: url(images/tab-left.jpg) no-repeat 0 1px;
}
Please note, as this is only an example, the CSS above isn’t complete and only shows how to apply the sliding doors hack.
You can see a live demo of a navigation menu which uses this technique on my blog here.
Using the sliding doors hack within WordPress
I saw many blog posts where some users told you to modify WordPress core in order to apply this technique. Personally, I never really liked this idea: First, the WordPress core wasn’t made for being modified. And secondly, if you do, when you’ll upgrade your WP version, you’ll have to re-modify the core. Not user friendly at all!
Instead of this, let’s use a regular expression, by using the php preg_replace() function. We are going to get our pages (or categories) without displaying it, and passing it as a parameter to preg_replace(). The two remaining parameters will be, of course, our regular expression: The pattern we’re looking for, and this pattern’s replacement.
To create this menu, paste the following code instead of the wp_list_pages() (or wp_list_categories()) function in the header.php of your WordPress theme.
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&exclude=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&exclude=181&title_li=&depth=1')); ?>
</ul>
Right now, your new menu is ready. You just have to make it sexy with CSS. Have fun!














There Are 64 Responses So Far »
Pingback: Skylog » Blog Archive » links for 2008-07-02
Pingback: CSS Techniques: Using Sliding Doors with Wordpress Navigation | CSS-FAQ
Pingback: WordPress Weekend Resources - July 4, 2008 | Theme Lab
Pingback: 10 tutos pour créer de sublimes boutons en CSS
Pingback: Top 10 CSS buttons tutorial list
Pingback: xeRo.Tumble
Pingback: CSS Techniques: Using Sliding Doors with Wordpress Navigation | blackleafmedia
Pingback: Selección de 10 tutoriales para crear botones con CSS » Cosas sencillas
Pingback: Top 5 WordPress Navigation Menu Tutorials
Pingback: WP Limits » Blog Archive » Navigation Tips: Suckerfish, Sliding Doors, and Breadcrumbs
Pingback: WordPress Developer’s Toolbox | Developer's Toolbox | Smashing Magazine
Pingback: links for 2008-09-17 | orioa
Pingback: WP?????-2.3: WordPress??????? | ????
Pingback: WordPress Developer’s Toolbox | rafdesign
Pingback: Joshua London » Blog Archive » Sliding Doors (kinda) for WordPress
Pingback: WordPress menu integration tutorial | Blog | CSS Menu Builder | Free online navigation generator
Pingback: How to: Use the CSS Sliding doors technique within WordPress
Pingback: 10 Killer WordPress Hacks | How-To | Smashing Magazine
Pingback: 10 Killer WordPress Hacks « ArticleSave
Pingback: 10 Killer WordPress Hacks | How2Pc
Pingback: 10 Killer WordPress Hacks | Web Hosting and Domains
Pingback: 30 Excellent CSS Based Navigation and Buttons Tutorial | instantShift
Pingback: 10 Killer WordPress Hacks | The Blog Specialist
Pingback: Mellowish » Blog Archive » 10 Cool WordPress Hacks
Pingback: 10 Killer WordPress Hacks | SuperBlog
Pingback: KurtQian’s blog - 10 Killer WordPress Hacks
Pingback: Top 50 Wordpress Tutorials - NETTUTS
Pingback: Top 50 Wordpress Tutorials
Pingback: 50???WordPress?? | ???
Pingback: 10????WordPress?? : ???????…
Pingback: 100+ Massive CSS Toolbox | tripwire magazine
Pingback: Top 50 WordPress Tutorials | The PhenixbluE
Pingback: 10 Killer WordPress Hacks | Bookmarks
Pingback: Hityun????? » 50???WordPress??
Pingback: Top 50 Wordpress Tutorials | Design-Tut+
Pingback: Breezy’s Wordpress » Blog Archive » Daily Digest for May 16th
Pingback: 22 CSS Button Styling Tutorials and Techniques : Speckyboy Design Magazine
Pingback: Top 50 Wordpress Tutorials
Pingback: More Tab Menu Hacks
Pingback: 30+ Useful WordPress Tutorials
Pingback: 50???WordPress?? | ????
Pingback: Techflaps | 15 Tutorials on Creating CSS Navigaton Menus with Sliding Doors Effect
Pingback: Top 5 Tutoriais Menú de Navegación para WordPress | Ajuda Wordpress em Português
Pingback: Top 50 Wordpress Tutorials « The Interweb of TJ
Pingback: 50 Wordpress Tutorials « Uttammitra’s Blog
Pingback: Using CSS Sliding Doors in WordPress Navigaton
Pingback: 22 tutoriels pour boutons CSS | WebCssDesign
Pingback: 30+ Useful WordPress Tutorials | The Apple Tech Blog
Pingback: Top 50 Wordpress Tutorials from TutPlus | WordPress News Magazine
Pingback: 155+ Mega CSS Roundup of Tools, Tutorials, Cheat Sheets etc. | tripwire magazine
Pingback: top 50 tutorials of wordpress(must use) | Umraz Ahmed | the official site
Pingback: Top 50 Wordpress Tutorials | NetExUrl | Web Development Tutorials & Design Resources
Pingback: 40+ CSS Based Navigation Tutorials Web Design Blog
Pingback: |??????| 50???WordPress?? : Welcome to www.studydog.net
Pingback: 185+ Very Useful and Categorized CSS Tools, Tutorials, Cheat Sheets | tripwire magazine
Pingback: WordPress – Snipers » Blog Archive » 40+ CSS Based Navigation Tutorials
Pingback: 15??????CSS???????? - IETester
Pingback: 185+ Very Useful and Categorized CSS Tools, Tutorials, Cheat Sheets « qeqnes | Designing. jQuery, Ajax, PHP, MySQL and Templates
Pingback: Designing A Perfect Navigation Menu Bar : Blog Design Series Part 3 | Creative Blogging Ideas
Pingback: 10 Killer WordPress Hacks « MalarVizhi
Pingback: 10 Adet Mükemmel CSS Menü Dersi | Sorbize
Pingback: 10 Adet Mükemmel CSS Menü Dersi « Web Önerilerimiz
Pingback: Geek is a Lift-Style. »Archive » 10 Killer WordPress Hacks
Pingback: 30 Excellent CSS Based Navigation and Buttons Tutorial | Dzinebook blog | Inspirational design resource for Web design - development - freelancer tips
Pingback: Top 50 WordPress Tutorials « Santosh Kori's Blog
Pingback: Top Collection Of WordPress Tutorials On The Web | Nerdy Geeks Blogging Guide
Pingback: Top Collection Of WordPress Tutorials On The Web | Nerdy Geeks
Pingback: 20 Excellent CSS Menu And Button Tutorials | VaneGraphics
Pingback: 22 CSS Button Styling Tutorials and Techniques
Pingback: 30 based on CSS navigation and button excellent design tutorial
Pingback: 40 Useful CSS Tutorials, Techniques And Resources | Free and Useful Online Resources for Designers and Developers
Pingback: 40 Useful layout by CSS Tutorials including Techniques And Resources « 68Design – (68ds.com) Creative Design Transmitter
Pingback: 30 Excellent Tutoriel de Navigation CSS et boutons | blog.lostwallpapers.com
Pingback: 60+ Awesome WordPress Tutorials | WebDino.net
Pingback: 10 Killer WordPress Hacks - Smashing WordPress
Pingback: Writing WordPress Guides For The Advanced Beginner
Pingback: Writing WordPress Guides For The Advanced Beginner - Smashing WordPress
Pingback: Writing WordPress Guides For The Advanced Beginner | Body Mind and Mood
Pingback: Writing WordPress Guides For The Advanced Beginner | TunerLabs Blog
Pingback: Bangladesh Web Lab | Bangladeshi Web Designer and Developer Blogs – Top 50 WordPress Tutorial
Pingback: Writing WordPress Guides For The Advanced Beginner | dot Key Plugins
Pingback: 30 Excellent CSS Based Navigation and Buttons Tutorial
Pingback: 100+ Resources for WordPress Theme Developers - | Web Help 101
Pingback: Writing WordPress Guides For The Advanced Beginner |Layout to HTML