Adding a top navigation on wordpress

  • Share
  • Share

introduction

What is Sliding doors?

Sliding doors CSS hack had been around for a long while. As this tutorial will walk you through how to add this hack to your site. The reason to add this quick and easy hack is because WordPress core functions wp_list_pages() and wp_list_categories() doesn’t support the function to add a span tag in this css menu. This hack is to insert the extra span which needed to create the menu.

Task needed to complete.

  1. Inserting PHP code to your header.php file
  2. Inserting the CSS to your style sheet

The Sliding doors top navigation hack for WordPress.

This is a strict hack to enhance the wp_list_pages() or wp_list_categories(), NO CHANGE OF CORE. Since wordpress update there security issue quite frequently, it is not ideal to change the core file period. In the below php code we will be using a regular expression of php preg_replace().

To create the menu we are going to use, let’s paste in the below code for your pages or categories in your header.php WordPress theme.

php code

To list your Pages:

<div id="dsnav" class="clearfix">
    <ul id="cssmenu">
    <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>
</div>

php code

To list your Categoreis:

<div id="dsnav" class="clearfix">
    <ul id="cssmenu">
    <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>
</div>

After the code

After the code had successfully been inserted to your code, you should have something similar to the image below.
step1
But now you will be saying, this is a bit ugly, I want something similar to your site or other website. So now we will need to include some css to get rid of the bullets and styling it like a menu. Let’s now take a look at the menu structure from the source code.

css style

For the one who doesn’t want to know the details of the css, who just want the code, here’s the whole chunk.

#dsnav {
	float:left;
	overflow:visible;
	padding:0;
	position:relative;
	width:100%;
	background:#999;
}
#cssmenu ul {
	margin: 0; /* all lists */
	padding: 0;
}
#cssmenu {
	height:30px;
	margin:0;
	overflow:hidden;
	padding:0 20px;
}
#cssmenu li {
	display:block;
	float:left;
	margin:4px 10px 0 0;
	padding:0;
}
#cssmenu li a {
	background:#CCC;
	-moz-border-radius:5px;
	-webkit-border-radius: 5px;
	color:#000000;
	display:block;
	font-size:80%;
	font-weight:normal;
	padding:4px 6px;
	text-decoration:none;
	text-transform:uppercase;
}
li.current-cat a{
	background:#000!important;
	color:#FFF!important;
}
#cssmenu li a:hover {
	background:#000;
	color:#FFF
}
li.current-cat a:hover{
	background:#333!important;
	color:#ccc!important;
}

css style explanation

The #dsnav id tag simply just added a 100% background to the back of your menu. This is basically the very bottom layer for your menu.

#dsnav {

	float:left;
	padding:0;
	margin:0;
	width:100%;
	background:#999;
}
.clearfix{
	clear:both;
}
The next step is to disable the bullets and inline all the menu list by block, and also we will want to transform all the text into uppercase.

#cssmenu li {
	display:block;
	float:left;
	margin:4px 10px 0 0;
	padding:0;
	text-transform:uppercase;
}
Next we will want to change the look of the a class.

  1. Add a background color
  2. Add the round corner for firefox and sarafi, IE are just not welcome for this class(sorry for the IE lover)
  3. Control the text color
  4. Display it in block, so this will clear off the spacing problem
  5. Create the font size, weight
  6. Add padding to your a tag so it is more roomy inside.
#cssmenu li a {
	background:#CCC;
	-moz-border-radius:5px;
	-webkit-border-radius: 5px;
	color:#000000;
	display:block;
	font-size:0.8em;
	font-weight:normal;
	text-decoration:none;
	padding:4px 6px;
}

After the code you will have something similar to this.
step2

The next step is to disable the bullets and inline all the menu list by block, and also we will want to transform all the text into uppercase.

#cssmenu li {
	display:block;
	float:left;
	margin:4px 10px 0 0;
	padding:0;
	text-transform:uppercase;
}
Next step, we will want to setup the spacing around the ul and the main cssmenu it tag.

#cssmenu ul {
	margin: 0; /* all lists */
	padding: 0;
}
#cssmenu {
	height:30px;
	margin:0;
	overflow:hidden;
	padding:0 20px;
}

step3
You should have some breathing space to your menu now.

Next step, we will want to add a hover state to the menu, so user will know where they are at. And add a current state to your category when user click on the link.

li.current-cat a{
	background:#000!important;
	color:#FFF!important;
}
#cssmenu li a:hover {
	background:#000;
	color:#FFF
}
li.current-cat a:hover{
	background:#333!important;
	color:#ccc!important;
}

final
Now you have the basic knowledge of css, do play around with your style. If you have any problem with css, please leave us a comment.

Related Posts Plugin for WordPress, Blogger...

I hope you have enjoyed this post, be sure to subscribe to my rss feed by click the subscribe button at the top.

Good Luck!

Be Sure to Subscribe to our feed or follow us on twitter to get the latest updates/patches
End of Article, Thanks for reading.
You can leave a response, or trackback from your own site.

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

4 Responses to “Adding a top navigation on wordpress

  1. Hiram Bolton says:

    Simply want to say your article is stunning. The lucidity in your post is simply impressive and i can assume you are an expert on this subject. Well with your permission allow me to grab your rss feed to keep up to date with incoming post. Thanks a million and please keep up the effective work.

  2. While doing research for adding rss to blog on Sunday your post regarding Adding a top navigation on wordpress | Designer Sandbox came up. Just wanted to drop a note to let you know what a great site you have. It is a great resource and a great place to drop by.

  3. Abhishek says:

    Thank u . It is awesome

  4. Bowmanave says:

    Great info! Thanks very much! Will give it a shot.

Leave a Reply