Here is the easiest way to display a homepage link in the navigation bar of your WordPress site. I can guarantee that you won’t find an easier way out there.

The code:

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

It’s pretty obvious how this code works. If the current page is the blog’s homepage, then it’ll add a special class to the list item to mark it as the current page. If it isn’t, the navigation button displays normally.

That’s it. No extra pages, no special configuration. The easiest way to display ‘Home‘ as a navigation link, period.

Post Revisions:

Tagged with:

About the author:

8 Responses to The easiest way to display ‘Home’ as a navigation link, Period.

  1. Pasquale says:

    You could always be an idiot and hardcode it.:P

  2. dRiZ says:

    How about just using:

    [code][/code]

    That works just the same!!! No reason to have all that code above! WP adds the current page item by default. x

  3. Simon says:

    I think there’s a slight typo in your code. The Span tag is breaking inside the li tag. I think you need to close the li tag before starting the span tag. Of course, you could get rid of the span tag completely, it doesn’t seem to be doing anything.

    Also, from a usability point of view, it’s always wise to not link to the page the user is currently on.

  4. Abba Bryant says:

    I would prefer myself ( keeping the above comment in mind also )
    [ul]
    [?php echo ( is_home( ) ) ? "[li class='current_page_item' ]Home[/li ]” : “[li][a href='{get_option(home)}']Home[/a][/li]“; ?]
    [?php wp_list_pages('title_li='); ?]
    [/ul]

    I hope this comes through alright. If it doesn’t, this is a perfect case for a ternary and the poster above is right, the home page should NOT have a link to the home page. A text representation is better in this case than a link.

  5. Abba Bryant says:

    the 2 blocks in [?php .. ?] tags are each 1 line. And all tags above are changed to square brackets for anyone who cares.

  6. @Simon: Thanks, should be fixed now.

    @Abba & Simon: How come? Why shouldn’t there be a link on the ‘home’ text?

  7. Tibor says:

    @Simon: I think Abba & Simon mean that it should not be a link when it is the current page; something I actually agree with.

  8. Tibor says:

    I meant @Michael ofcourse..

Leave a Reply

Please note that WPCandy is a moderated community.

 

Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>