I am looking to add the nofollow attribute to my menu items but this doesn't seem possible right now. Is there some type of module that handles this?

Thanks

Comments

dwees’s picture

What you want to do is:

Put the following function at the end of your template.php file (before the closing ?> tag if you have one):


function phptemplate_menu_item_link ($item, $link_item) {
  $no_follow_links = array(
    'put',
    'the',
    'titles',
    'of',
    'the',
    'menu',
    'items',
    'you',
    'want',
    'to',
    'make',
    'nofollow',
    'here',
  );

  if (in_array($item['title'], $no_follow_links)) {
    return l($item['title'], $link_item['path'], !empty($item['description']) ? array('title' => $item['description'], 'rel' => 'nofollow') : array('rel' => 'nofollow'), isset($item['query']) ? $item['query'] : NULL);
  } else {
    return l($item['title'], $link_item['path'], !empty($item['description']) ? array('title' => $item['description']) : array(), isset($item['query']) ? $item['query'] : NULL);
  }
}

assuming you are using the phptemplate system for your site. Hope this works, I haven't tested it.

Dave

TiViTi’s picture

is there another solution ?

davidkyte’s picture

Hi this worked great for the navigation menus, however it had no effect on the primary menu items. Is there an equivalent override function for the primary menu?

Thanks for the posting ;-)

Dave

dwees’s picture

Try phptemplate_links, but copy the code from the theme_links function, and adjust it accordingly.

Dave

My site: http://www.unitorganizer.com/myblog

quotesbro’s picture

Somebody has made it for Drupal 6?

oeroek’s picture

Take a look at the following module. http://drupal.org/project/menu_attributes

It ads several atributes to every menu item such as target="", rel="" and also id="" so you can easier style the items with CSS.

jasom’s picture

To specific paths:

function THEMENAME_preprocess_menu_link(&$variables) {
    $system_paths = array(
        'node/22',
        'taxonomy/term/11',
    );

    if (in_array($variables['element']['#href'], $system_paths)) {
        $variables['element']['#localized_options']['attributes']['rel'][] = 'nofollow';
    }
}

To all menu items:

function THEMENAME_preprocess_menu_link(&$variables) {
        $variables['element']['#localized_options']['attributes']['rel'][] = 'nofollow';
}

To all menu items from specific menu (menu-footer):

function THEMENAME_preprocess_menu_link(&$variables) {
    //for Footer Menu ('menu-footer')
    if ($variables['element']['#theme'] == 'menu_link__menu_footer') {
        $variables['element']['#localized_options']['attributes']['rel'][] = 'nofollow';
    }
}