Wordpress Tutorial: Navigation with current page highlighting

I’m pretty decent at web design, but up until today, I didn’t really know how to do current page highlighting in the correct manner.  I was using an extremely convoluted method that used a buttload of PHP if() and elseif() tags.  Today I finally learned how to use WordPress’s built in functionality to achieve the same effect with a lot less work.

I know a few people that have seen this page are learning CSS, as they’ve found this site on the CSS-Tricks website, where I frequent the message boards.  Therefore, I present to you this WordPress/CSS/PHP tutorial.

Click the “Read More” link to check it out! 

For the purposes of this tutorial (and also because I’m not too experienced at writing tutorials), I’m going to assume a working knowledge of CSS.  Some PHP knowledge will help, but for this tutorial I think I’ve got you covered.  If you don’t know CSS, W3schools has a section devoted to teaching it.  If you’re trying to build a WordPress theme from scratch, there’s a good tutorial e-book that you can download at the bottom of this link.  We’re going to jump right in with displaying the links.

To display the page links, you need this code:

<ul>
<?php wp_list_pages(); ?>
</ul>

That will display your links in an unordered list.  When you view the source, you’ll see that the list elements have classes of “page_item page-item-#”.  The current page you’re viewing will have an additional class of “current_page_item”.  That’s what we’re going to use in our style.css file to create the current page highlighting.

In your CSS file, the following will cause your page links to be white with over and underlines when hovered (just like the links on this site), and will cause them to be yellow when you’re currently viewing that page.

 /* non-current page links */
.page_item a {
color: #ffffff;
text-decoration: none;
}
.page_item a:hover, .page_item a:active {
text-decoration: overline underline;
/* current page links */
.current_page_item a {
color: #fff000;
text-decoration: none;
}
.current_page_item a:hover, .current_page_item a:active {
text-decoration: overline underline;
}

In your page_item element, you can define the style and look of your links as you see fit, but we’re not going to get into that right now.

I know what you’re thinking though - “but JoE, that’s all well and good for my page links, but what if I want a home link in the same navigation section?”

Fine.  I’m going to show you some PHP code here, which will allow you to add a “home” link that works with the link styling shown above - just like on my site!

You’ll recall that when we put the tag to get the links, we enclosed it in some <ul> tags. To illustrate this properly, I will show those tags again in the following example. Here goes:

<ul>
            <? if(is_home()) { ?><li class="page_item current_page_item"><a href="<? bloginfo('url'); ?>">Home</a></li>
            <? } else { ?><li class="page_item"><a href="<? bloginfo('url'); ?>">Home</a></li><? } ?>
            <?php wp_list_pages('title_li= '); ?>
        </ul>

So what in the hell is going on here? We start our unordered list in line one. In line two, we use the PHP if() call to find out if we’re on the homepage, using WordPress’ is_home() function as our argument. On this line, if it’s true that you’re looking at the home page, we’re going to give our list element a class of “page_item current_page_item.” That will trigger the “.current_page_item a” element from our stylesheet, rendering the “Home” link yellow, setting it apart from the other links. On line three, we use the “else” statement to say say that the case in line two isn’t true. This will only give our list element a class of “page_item,” leaving it to render in the same manner as the other site links other than the current page. At the end of the line, we use “}” to close the PHP “else” statement. On line four, we continue as normal with our list of pages - though we’ve added in “title= ‘” into that statement, which gets rid of the “PAGES” title. You can put that above your home link by using <li>PAGES</li>.

Hope this helps!

Posted under the category "Tutorials" | Leave A Comment

Feelin' Uninspired

I feel like designing something, but I’m so uninspired right now.  Don’t know why.  Meanwhile, I’m watching the E! True Hollywood Story of Hulk Hogan.  If anybody wants to give me an idea for a project to work on, let me know.  Until then, I guess I’m watching the Hulkster on E!

Posted under the category "Miscellaneous" | Leave A Comment

So much for that idea...

Well, I was going to start up a twitter account, and mess around with putting a twitter thing in the sidebar over there.  Didn’t work.  Well, signing up for the Twitter account worked, but searching for my friends didn’t.  So, as such, for the moment you won’t be seeing any tweets on this site.  Maybe if they fix it soon I’ll consider actually using the service.  Anyhow, I’ve got work to do.  Later guys!

Posted under the category "Miscellaneous" | Leave A Comment

If anybody is wondering...

I'm currently accepting clients for web design, press release writing, and promotional flier design. You can obtain my services and see my portfolio over at Fropac.com. Pricing info is in one of the latest posts over there, but I'm willing to negotiate on them.  Thanks for checking out my new blog!

Posted under the category "Design Projects" | Leave A Comment

ThinkSoJoE's Thoughts launches!

There’s really no rhyme or reason for this site, I was just bored and sitting around all day - so I decided to try my hand at a new web design idea I had.  I’ll be adding some more info about myself, and will probably use this as more of a portfolio than anything.  Also, I’ll be using it to rant.  Hope you guys like the design!

Posted under the category "Miscellaneous" | Leave A Comment

« Newer 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Older »