Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I use a menu side-wide but I want to make some menu items nofollow on single post only. Can any one help me how to do it?

Updated: I used the below mentioned code and it worked perfectly. Because I am new to wordpress, so please tell me if anything is wrong in it. [sorry for bad english]

add_filter('walker_nav_menu_start_el', 'nofollow_menu_items', 1, 4);
function nofollow_menu_items($item_output, $item, $depth, $args) {
if( is_single( ) )
$nofollow = array(258,261,262); // Menu item id's (View page source and menu-item-123)
$location = ''; // Use 'primary' to only filter header menu in twentyten
$menu = ''; // Use menu names to filter by menu

if(
    in_array($item->ID, $nofollow)
    && (!empty($location) && $args->theme_location == $location || empty($location))
    && (!empty($menu) && $args->menu == $menu || empty($menu))
) {
    $item_output = str_replace('<a ', '<a rel="nofollow" ', $item_output);
}


return $item_output;

}
share|improve this question
I used this code and it worked perfectly. But I am new to wordpress, so please tell me if anything is wrong in it. – Aamir Oct 15 '11 at 20:37
Did it stop working? When? What did you change? Did you try it with the default theme and no plugins activated? – kaiser Apr 11 '12 at 12:14

closed as not a real question by toscho Jul 18 '12 at 21:28

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, see the FAQ.

1 Answer

Yoast SEO plugin http://wordpress.org/extend/plugins/wordpress-seo/ allows to set nofollow for single post and has a lot of other useful options.

share|improve this answer
1  
But I don't want to use any plugin. And moreover it doesn't support custom Menus as far as I know. Anyways this code is working perfectly. – Aamir Oct 17 '11 at 14:20

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