0

I am trying to add rel="nofollow" attribute to menu links output by the menu_block module.

My core menu is called Main Menu, which I do not want to modify as it is used for main navigation. However the product level of this menu is output in the footer by Menu Block module, and it is these links I want to add an attribute to.

Thanks.

1

If you use different menus then:

There's a module for that(tm) Menu Attributes

If it's the same menu then things get farm more complicated as you'll have to implement some hook. Take a look at hook_menu_block_tree_alter in menu_block.api.php.

  • Thanks mariomc. I had a look at these hooks but there's not much documentation for them. I'll have another go. – davewilly Jul 3 '13 at 16:51
  • It's looking unlikely it's achievable since the two menus on the page use the same $tree array which is available in hook_menu_block_tree_alter. So it adds the attribute to both menus. – davewilly Jul 3 '13 at 17:49
  • @davewilly Menu Attributes works in per-item basis, so if you render one level in main, and lower level in block, it should do the trick. Anyway, why would you nofollow your own internal links? It only leads to pagerank evaporation now. – Mołot Jul 4 '13 at 9:02
  • @Mołot I display Product Categories in the Main Menu and again with their children in the footer navigation. I am adding nofollow as when I removed them footer links, the site moved up 2 pages in the SERPS, and back again when I re-added them. So I believe it is excessive link penalty. What ever the case, it's having an effect. Here is illustration to help: i43.tinypic.com/2gtxnwk.png – davewilly Jul 4 '13 at 10:32
0

Something like this for Navigation Menu (navigation):

function THEMENAME_preprocess_menu_link(&$variables) {
    //for Navigation Menu ('navigation')
    if ($variables['element']['#theme'] == 'menu_link__navigation') {
        $variables['element']['#localized_options']['attributes']['rel'][] = 'nofollow';
    }
}

Something like this for Footer 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';
    }
}

Something like this for speficit paths:

function THEMENAME_preprocess_menu_link(&$variables) {
    $system_paths = array(
        'node/56',
        'taxonomy/term/56',
        //more-your-paths
    );

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.