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

    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:

    Posted January 31, 2008

8 comments

  1. Pasquale said:

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

    on January 31, 2008 at 3:58 am Reply

  2. dRiZ said:

    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

    on January 31, 2008 at 11:17 am Reply

  3. Simon said:

    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.

    on January 31, 2008 at 4:41 pm Reply

  4. Abba Bryant said:

    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.

    on January 31, 2008 at 9:34 pm Reply

  5. Abba Bryant said:

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

    on January 31, 2008 at 9:34 pm Reply

  6. Michael Castilla said:

    @Simon: Thanks, should be fixed now.

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

    on February 1, 2008 at 12:29 am Reply

  7. Tibor said:

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

    on February 2, 2008 at 2:12 am Reply

  8. Tibor said:

    I meant @Michael ofcourse..

    on February 4, 2008 at 12:17 pm Reply


RSS feed for comments on this post.

Leave a comment

Trackbacks on this post

    TrackBack URL