Saturday, 18 December 2010

How to make the Home link (primary link) active.

This is what I did to make the home link active on the front page which is a static page (ie node/1).
1: Create a static page. ie go to http://www.example.com/node/add/page
and follow the following steps:
a) Create a page with title as Home .
b) Enter the HTML in the body and submit the node.
2: Then go to Site information at http://www.example.com/admin/settings/site-information and change
the default front page to:
http://www.example.com/node/Node-Number
Node-Number=Node Number of the home page created.
3: Then go to Menus at http://www.example.com/admin/build/menu and create a new primary link
with the following details:
a)Title:Home
b)path: <front>
c)Parent item:Primary Links
and then submit the menu item.
4:Now open the template file of your current custom theme and add the following function at the bottom of the page
(I got this code from one of the forums)

<?phpfunction phptemplate_links($links, $attributes = array()) {

  if (!
count($links)) {
    return
'';
  }
 
$level_tmp = explode('-', key($links));
 
$level = $level_tmp[0];
 
$output = "<ul class=\"links-$level ".$attributes['class']. "\">\n";
  foreach (
$links as $index => $link) {
   
$output .= '<li';
    if (
stristr($index, 'active')) {
     
$output .= ' class="active"';
    }
// frontpage AND current-link in menu is <front>
   
elseif((drupal_is_front_page()) && ($link['href']=='<front>')){
     
$link['attributes']['class'] = 'active';//add class active to <li
     
$output .= ' class="active"';//add class active to <a
   
}
   
$output .= ">". l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment']) ."</li>\n";
  }
 
$output .= '</ul>';

  return
$output;
}
?>
5: Now open your page.tpl.php and add/check for the following line in the header region

  <div id="vl-links">
     //check for this line
        <?php if (isset($primary_links)) : ?> //check for this line
                <?php print theme('links', $primary_links, array('class' => 'links primary-links')) ?>
        <?php endif; ?>
  </div>
6: If you have followed all the steps correctly you should be able to see the home link active and inactive.

No comments:

Post a Comment

Source base installation of php, mysql and apache in ubuntu/ linux

Compile and Install a LAMP(Linux/Apache/MySQL/PHP) Server from Source In the last post, I described the method to install a LAMP ser...