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;
?>

Wednesday, 26 September 2012

How to create a new page template in Wordpress?

In wordpress usually there is no option to select page template after a new installation. It will be usually located in the Edit page section of wordpress backend. Right side of this page you will find Page attributes with Parent and Order section. Once you created a new page template there will be another option called Template(Drop down).

Navigate to themes your folder which looks like this wp-content\themes\urtheme.
create a new php file or copy the existing page.php file in the same folder with your new template name. Then open that file and place the below code at the top of the page.
<?php
/*
Template Name: Home
*/


Home is the new template you created. Just go the Edit page of backend and you'll find template under Page attributes. There you can select your template for the page. That It! Then you can modify the new template you created as you wish.