Friday, 28 September 2012

How to add Home link to wp_nav_menu() in Wordpress ?


If you want add “Home” link to your wordpress wp_nav_menu() function then you have to add following code inside your themes function.php file.
add_filter( 'wp_nav_menu_items', 'add_home_link', 10, 2 );
function add_home_link($items, $args) {
  
        if (is_front_page())
            $class = 'class="current_page_item"';
        else
            $class = '';
  
        $homeMenuItem =
                '<li ' . $class . '>' .
                $args->before .
                '<a href="' . home_url( '/' ) . '" title="Home">' .
                $args->link_before . 'Home' . $args->link_after .
                '</a>' .
                $args->after .
                '</li>';
  
        $items = $homeMenuItem . $items;
  
    return $items;
}
Then call the navigation menu in header.php like this:
<?php
if(function_exists('wp_nav_menu')):
wp_nav_menu(
array(
'menu' =>'primary_nav',
'container'=>'',
'depth' => 1,
'menu_id' => 'menu' )
);
else:
?>
<ul id="menu">
<li><a href="<?php bloginfo('url'); ?>" title="Home">Home</
a></li>
<?php wp_list_pages('title_li=&depth=1'); ?>
</ul>
<?php
endif;
?>

No comments:

Post a Comment