Highlight current page in WordPress menus

WordPress has a lot of built in functionality that you can tap for advanced customization. One of these which is often ignored is the ability to highlight the current page in menus with CSS. By default, WordPress assigns a special style class to the button that points to the current page. By styling this class differently from the “regular” classes the current page button is highlighted.

In this tutorial we’ll look at how to utilize the built in current page styling, how to create a menu like the one you see above and also how to hard code the functionality into a menu that is not generated dynamically by WordPress.

Identifying the custom styles

If you view the source code of a WordPress blog with a menu like the Reader’s Companion you’ll see that the menu items have been given different classes automatically by the CMS:

<div id="menu">
<ul>
	<li class="page_item current_page_item"><a href="http://expression.pinkandyellow.com">home</a></li>
	<li class="page_item page-item-9"><a title="Lesson Files" href="http://expression.pinkandyellow.com/lesson-files/">Lesson Files</a></li>
	<li class="page_item page-item-11"><a title="About the Author" href="http://expression.pinkandyellow.com/about-the-author/">About the Author</a></li>
	<li class="page_item page-item-13"><a title="FAQ" href="http://expression.pinkandyellow.com/faq/">FAQ</a></li>
	<li class="page_item page-item-15"><a title="Further Reading" href="http://expression.pinkandyellow.com/further-reading/">Further Reading</a></li>
	<li class="page_item page-item-17"><a title="Contact" href="http://expression.pinkandyellow.com/contact/">Contact</a></li>
</ul>
</div>

The important classes here are page_item which styles all the menu items and current_page_item which is only applied to the current page item. By styling these two classes independently you can get some very nice results. To demonstrate how far you can take this I’ve made a mock-up page that simulates the end output of a WordPress menu with custom styling. You can find it here.

By taking a closer look at the CSS you’ll see that the page_item and current_page_item classes have different styling. The remainder of the code is set up to make the button backgrounds expand and contract in accordance with the Sliding Doors technique from A List Apart.The CSS code on the demo page is identical to the CSS code being used to style the page itself:

page_item styles:

.page_item a {
float: left;
display: block;
background: url("img/right-over.png") no-repeat right top;
padding: 6px 15px 7px 6px;
text-decoration: none;
font-weight: bold;
color: #DDDDDD;
text-transform: uppercase;
}
 
.page_item a:hover {
color:white;
}

current_page_item styles:

.current_page_item {
float: left;
background: url("img/left-selected.png") no-repeat left top;
margin: 0;
padding: 0 0 0 8px;
}
 
.current_page_item a, .current_page_item a:hover {
float: left;
display: block;
background: url("img/right-selected.png") no-repeat right top;
padding: 6px 15px 7px 6px;
text-decoration: none;
font-weight: bold;
color: #C7532D;
text-transform: uppercase;
}

If you already have your menu up and running properly, all you need to do is style these two classes and you’ll automatically have the current page highlighted in your menu. But this only applies to dynamic menus generated by WordPress. What happens when you hard code your menus yourself?

Assign dynamic styles to hard-coded menus

To get the same effect when you create a hard-coded menu you need to apply some clever PHP code to each of your menu items.

If you don’t understand what I mean by “hard-coded menu” consider this: By default your menu is called by a function within WordPress that lists all of your pages plus the Home page. This call usually looks something like this:

<?php wp_list_pages('title_li=&depth=1'); ?>

But this function lists all your pages. If you only want certain pages listed you have to take this function out and create a manual list yourself. This is a hard-coded menu.

To make the current_page_item class apply only to the current page item you need to add a small piece of PHP to each of your menu items. This line of code looks like this:

<?php if (is_page('home')) { echo "current_page_item"; }?>

and is applied inside the li tag of each item. To give a concrete example I have added this code to the list items in the list example at the beginning of this tutorial:

<div id="menu">
<ul>
	<li class="page_item <?php if (is_page('home')) { echo "current_page_item"; }?>"><a href="http://expression.pinkandyellow.com">home</a></li>
	<li class="page_item page-item-9 <?php if (is_page('Lesson Files')) { echo "current_page_item"; }?>"><a title="Lesson Files" href="http://expression.pinkandyellow.com/lesson-files/">Lesson Files</a></li>
	<li class="page_item page-item-11 <?php if (is_page('About the Author')) { echo "current_page_item"; }?>"><a title="About the Author" href="http://expression.pinkandyellow.com/about-the-author/">About the Author</a></li>
	<li class="page_item page-item-13 <?php if (is_page('FAQ')) { echo "current_page_item"; }?>"><a title="FAQ" href="http://expression.pinkandyellow.com/faq/">FAQ</a></li>
	<li class="page_item page-item-15 <?php if (is_page('Further Reading')) { echo "current_page_item"; }?>"><a title="Further Reading" href="http://expression.pinkandyellow.com/further-reading/">Further Reading</a></li>
	<li class="page_item page-item-17 <?php if (is_page('Contact')) { echo "current_page_item"; }?>"><a title="Contact" href="http://expression.pinkandyellow.com/contact/">Contact</a></li>
</ul>
</div>

When the page is loaded the PHP script looks to see if the page name matches the name in the is_page section and if it does, the current_page_item class is applied. Therefore the name you put in the is_page section of the PHP script must match the name of the page as created in WordPress exactly. If it does, the current page item will be highlighted dynamically.

Post a job. Find one. authenticjobs.com

Read my article in the Microsoft Expression November Newsletter

A month or so ago Microsoft contacted me and asked if I would write an article on Expression Web for their Expression Newsletter. How could I say no to such an opportunity? After some back and forth about the topic I landed on an article on how to create a Pure CSS Drop-Down menu. Over time I’ve encountered numerous solutions, most of which were clunky and didn’t work properly. So the tutorial demonstrates how to create a fully functional CSS-only drop-down menu utilizing the excellent CSS features in Expression Web.

That’s not to say you have to use Expression Web to get something out of the article though. You can follow the tutorial and get the same results even if you’re using Notepad to build your sites. It’s just that Expression Web makes it a hell of a lot easier to manage.

This article comes hot on the heels of my book Sams Teach Yourself Microsoft Expression Web 2 in 24 Hours which contains an expanded tutorial on the same topic that also covers a layers-based drop-down menu. If you are an Expression Web 2 user of if you are considering buying or switching over to this excellent web authoring and publishing platform I humbly suggest you pick up a copy for yourself. It’s a quick read and it gives you hour-by-hour instructions on how to create a web site from scratch with the application. And once you’re done, you’ll have a fully working and standards based web site at your disposal. I wrote it as the book I wish someone had written when I started out and from the response I’ve gotten so far people are learning a lot from it. Which is what I set out to do.

If you’re interested in a preview of the kind of content you’ll find in the book or you just want to know how to make a Pure CSS Drop-Down menu, you can read the full article, The No-Code Way to a Pure CSS Horizontal Drop-Down Menu with Expression Web, here or subscribe to the Expression Web Newsletter.

6 Must Have WordPress Plug-ins

WordPress is a powerful blogging platform and CMS that can be used for simple blogs as well as advanced websites. One of the things that makes WordPress my chosen platform for all my projects - from personal blogs to massive business websites - is the seemingly endless expandability through free 3rd party plug-ins. Over time I’ve tried many and discarded most, but some have stood the test of time and become compulsory installs in all the WordPress projects I work on.

Here is the list of Pink & Yellow approved must-have WordPress Plug-ins:

Akismet - Ninja Spam Killer

Since this blog was launched in February 2008, Akismet has caught an incredible 20,800 spam comments. At present it filters out about 100 - 200 spam comments per day. With such a high number you would think a lot of the comments are “false positives” - real comments mistaken for spam - but that’s just not the case. So far Akismet has been right 98% of the time and has saved me countless hours of sifting through garbage comments.

Akismet comes standard with WordPress. To activate it you need a WordPress.com account to generate an API key so your spam results can be tracked.

All In One SEO Pack - Get noticed

WordPress’ SEO is mindboggling to begin with. I’ve launched blogs that were listed on Google less than 30 minutes later without me lifting a finger! But unless you add an SEO plug-in, you have little control of how the meta data for your blog appears. Well, look no further than the All In One SEO Pack. Just like the name says this plug-in is an all-in-one solution to your problems. It allows you to define meta data such as title, description and keywords for any and all pages and posts in your blog or site independently of the actual page content. In addition it lets you set a default title, description and keywords for your blog or home page so you don’t get a pile of different stuff from all the different posts featured there. The true power of this plug-in can be seen in many of the posts on this blog where the page title (seen at the top of the browser window or browser tabs) and description is different from the title and body of the post itself. This is ideal if you want to push or customize only the most interesting or most important info to search engines for more hits.

Sociable - All the social networks in one place

A quick way to get some attention for your blog or website is by getting people to list it in social networks. However, this only happens if people either have browser applications installed or take the time to manually post your links to these sites. To make this step easier and increase the chance of getting posted you should add bookmarking buttons directly on your pages and posts. And that’s what Sociable does. This plug-in serves up a huge and expanding list of social networks with icons that can be placed at the end of every post or only on certain posts as defined by you. It is easy to manage and quite effective when implemented. Unfortunately it also clashes with OIO Publisher, a popular advertising plug-in that I use on this blog. For this reason Sociable is currently offline here at Design is Philosophy. Hopefully this will be solved shortly and I can get the tags back on my posts for some increased attention.

WordPress Automatic Upgrade

One of the great things about WordPress is that it is constantly updated and improved. Just this year alone we have seen what… 5 or 6 new releases? But while updates are great, actually performing the task of updating your blog can be a bit of a pain, especially if it is highly customized like this one is. Well fret not my fried: WordPress Automatic Upgrade is here to help. This plug-in guides you through a complete upgrade of your WordPress powered site from right inside the WordPress admin panel. Through a series of steps it lets you run backups of important files and your database, deactivates plug-ins, takes the site offline while the update is happening, downloads the latest version of WordPress, updates the application, reactivates the plug-ins and puts the site back online. The whole process takes two minutes tops. Which is far less than the manual upgrade procedure. And it keeps all your custom settings and themes intact.

WordPress.com Stats - Keep track of your visitors from within your blog

There are hundreds of excellent site stats services like Google Analytics out there for you to use. But they all require you to go to a separate service to track your visitor activity. WordPress.com Stats runs stats from within your blog and lets you access them right from your dashboard. Your blog is tracked via the same API Akismet uses and by applying the same API to numerous blogs you can get stats from all of them from within any of the other ones. Very convenient.

WordPress.com Stats tracks visits as well as page views and gives you stats on the most popular pages, search engine search words, incoming links, clicked links and a lot of other info that gives you a better idea of how your visitors are using the site. It is by no means the perfect solution, but neither is most of the other options out there. So for pure convenience it is great.

WP-Syntax - Display code as code

When you write coding tutorials it is important to not only make the code stand out visually from the rest of the text but also to help the reader understand what kind of code it is they are looking at. WP-Syntax does just that. This plug-in styles a huge list of code languages with colours, tabs and markers to make it easier to read and easier to display. I use it heavily in my tutorials and it works great once you figure out how to use it properly. WP-Syntax runs an independent style sheet so you can also customize the styles for your own site if you want. The only catch is that you have to write your posts in HTML view to avoid massive coding conflicts. But that’s a small price to pay for a professional look and better user interaction.

These are the plug-ins I always install on blogs and sites I create. If you haven’t been there before I recommend you take a look at the plug-ins page in WordPress Extend where thousands upon thousands of plug-ins for pretty much every purpose is served up free of charge.

My list is by no means a complete and I’m sure there are many other plug-ins out there that deserve some attention. If you know of any or think I’ve missed some, post them in the comments below and I’ll take a closer look.

Create a Twitter Box in Your Sidebar - Part II

My Sidebar Twitter box tutorial seems to have struck a chord with WordPress users and it has generated some interesting questions. One of them, from TheNext2ShineBlog posed an interesting problem I decided to look into in more detail:

the only thing I would like to change is the time aspect (23 days ago // 4 hours ago). Is there a css code to hide that link without taking away the links from the original twitter message?

What TheN2S is refering to is the tail end of each Twitter message that reads either “less than a minute ago”, “a few minutes ago” etc up to “X days ago”.

Careful inspection of the JavaScript that generates the Tweets for the application (found here) shows that the time information is a core function of the Twitter system so it is coded into the main structure of the application itself. Therefore it is hard to siply remove it unless you want to create your own custom JS. But TheN2S is on the right track in asking if it can be removed by way of CSS.

Lifting a random tweet off my own site I found that the main body the JS spits out is contained within a span tag while the tail end with the time info is not:

<li>
   <span>@<a href="http://www.twitter.com/webb_art">webb_art</a> DropBox works well for me and is platform independent: <a href="http://www.getdropbox.com/">	http://www.getdropbox.com/</a></span>
   <a href="http://twitter.com/mor10/statuses/1003495851" style="font-size: 85%;"> about 15 hours ago</a>
</li>

That means we can use CSS to hide the content not in the while maintaining the visibility of the content that is. That requires some additions to the original CSS code:

#twitter_div ul li span {
	visibility: visible;
}
 
#twitter_div ul li span a {
	color: #D78E42;
	visibility: visible;
}
 
#twitter_div ul li a {
	visibility: hidden;
}

The first two single out the regular and link contents within the span specifically and set their visibility to visible. This is done because the last style sets the visibility of all anchors within list items under the twitter_div ID to hidden. So we are really working backwards - first hiding everything and then unhiding it in particular cases.

By adding these three style elements the time information will be hidden by the CSS while everything else shows up normally.

Tagged , ,

A better desktop with RocketDock and Vista Start Menu

Sitting in front of a computer about 80% of your working day it is important that your work environment - that is your desktop - is both visually pleasing and functional. Having switched to Windows Vista not too long ago and just bought a new laptop I spent some time customizing my desktops to increase functionality and decrease clutter. Along the way I found some useful applications and some nice tricks that I’d like to share with you.

Get a Dual-Monitor Wallpaper with DisplayFusion

One of the things that has bugged me from day one was that out of the box neither XP nor Vista allowed you to have different wallpapers on different screens if you have a dual screen setup. I’ve been working with two screens for years and a cohesive background graphic for my workspace has always been one of the items on my wishlist. When I came across the beautiful Mandolux multi-monitor wallpapers I decided that now was the time so I started digging around on the web for a small app that would let me split my desktop in half so to speak. After some trial and error I landed on DisplayFusion - a free multi-monitor desktop wallpaper application that runs on both XP and Vista alike. The application is light and easy to use and combined with a Mandolux wallpaper the result (as seen at the top of this article) is quite stunning.

Keep your tools handy with RocketDock

It’s no secret that I’m a Windows guy and I have less than kind things to say about it’s rival the Mac. But that doesn’t mean I’m not willing to say that Steve Jobs and his fruity company hasn’t come up with some briliant ideas. Ideas like the customizeable launch bar. Fortunately clever coders have created several clones of this application that run in Windows environments. One of these is the nice and spiffy RocketDock. It lets you drag and drop any application, folder or whatever else comes to mind into a dockable launch box and makes them available to you with one click. I’ve installed it on both my office computer and my laptop (as seen below) and moved the regular Windows Taskbar to the left hand side to get it out of the way. Now I have all my frequently used applications handy with one click at the bottom of my screen and if I want to dig deeper I can always go to the Taskbar. As Candide would say, it’s the best of all possible worlds.

Harness the launching power of the Vista Start Menu

An often overlooked application that I myself wasn’t fully aware of until recently is the Vista Start Menu search box. More than just a regular search box, this powerful feature lets you launch any application by simply writing (part of) it’s name and hitting Enter. This comes in handy when you want to launch a seldom used or hidden application like the equally genius Snipping Tool which for some bizarre reason is hidden within Vista. The search box catalogues all your applications and lets you launch them without digging through folder trees on the Program menu.

“But wait. That’s exactly what Launchy does” you might say (if you’re a real nerd or a Lifehacker reader). And you are right. But think about this for a second: Why would you use a third party application to do something that has been built into the operating system anyway? Sadly the prevalence of Launchy and applications like it on Vista systems shows how buggy the transition from XP to Vista has been. But fret not: If you’re already a Launchy user, try switching over to the Vista Start Menu for a while and you’ll see that you can safely get rid of that extra 3rd party bulk and still get pretty much the same results.