I want to add the nofollow tag to the comments links on my website: http://www.hannahmontanazone.com both for individual posts and for the comment links in the footer. I think it would be good because I have way too many links on one page. So anyway, the following bit of code determines these contents: <?php if (function_exists('mdv_recent_comments')) { ?> <div class="Cols" style="margin:0px 30px"> <h3>Recent Comments</h3> <ul> <?php mdv_recent_comments('10'); ?> </ul> </div> <?php } ?> PHP: How can I add nofollow to the links produced by this code ?
just on the comment page where these links are appearing in <head> section of page use Meta tag for nofollow. Regards Alex
If you want only certain links on the page to have the nofollow attribute, you'll need to edit the mdv_recent_comments function. Can you show us the contents of that function?
Yes, I only want these comments to have nofollow links so I can at least minimize the outgoing links on my page. Here is that function I think: /* Plugin Name: Recent Comments Plugin URI: http://mtdewvirus.com/code/wordpress-plugins/ */ if (function_exists('mdv_recent_comments')) { }else{ function mdv_recent_comments($no_comments = 10, $comment_lenth = 5, $before = '<li>', $after = '</li>', $show_pass_post = false, $comment_style = 0) { global $wpdb; $request = "SELECT ID, comment_ID, comment_content, comment_author, comment_author_url, post_title FROM $wpdb->comments LEFT JOIN $wpdb->posts ON $wpdb->posts.ID=$wpdb->comments.comment_post_ID WHERE post_status IN ('publish','static') "; if(!$show_pass_post) $request .= "AND post_password ='' "; $request .= "AND comment_approved = '1' ORDER BY comment_ID DESC LIMIT $no_comments"; $comments = $wpdb->get_results($request); $output = ''; if ($comments) { foreach ($comments as $comment) { $comment_author = stripslashes($comment->comment_author); if ($comment_author == "") $comment_author = "anonymous"; $comment_content = strip_tags($comment->comment_content); $comment_content = stripslashes($comment_content); $words=split(" ",$comment_content); $comment_excerpt = join(" ",array_slice($words,0,$comment_lenth)); $permalink = get_permalink($comment->ID)."#comment-".$comment->comment_ID; if ($comment_style == 1) { $post_title = stripslashes($comment->post_title); $url = $comment->comment_author_url; if (empty($url)) $output .= $before . $comment_author . ' on ' . $post_title . '.' . $after; else $output .= $before . "<a href='$url' rel='external'>$comment_author</a>" . ' on ' . $post_title . '.' . $after; } else { $output .= $before . '' . $comment_author . ': <a href="' . $permalink; $output .= '" title="View the entire comment by ' . $comment_author.'">' . $comment_excerpt.'</a>' . $after; } } $output = convert_smilies($output); } else { $output .= $before . "None found" . $after; } echo $output; } } PHP: Is this the right one ? This is for wordpress by the way so I took it from the theme file.
Come to think of it you might need it on the also. But I'm not sure you can have two "rel" attributes in one link.
This is what I would do: Insert this copy of the existing mdv_recent_comments function, just below where the original mdv_recent_comments function is defined. /* Plugin Name: Recent Comments Plugin URI: http://mtdewvirus.com/code/wordpress-plugins/ Edited to add attribute nofollow */ if (function_exists('mdv_recent_comments_nofollow')) { }else{ function mdv_recent_comments($no_comments = 10, $comment_lenth = 5, $before = '<li>', $after = '</li>', $show_pass_post = false, $comment_style = 0) { global $wpdb; $request = "SELECT ID, comment_ID, comment_content, comment_author, comment_author_url, post_title FROM $wpdb->comments LEFT JOIN $wpdb->posts ON $wpdb->posts.ID=$wpdb->comments.comment_post_ID WHERE post_status IN ('publish','static') "; if(!$show_pass_post) $request .= "AND post_password ='' "; $request .= "AND comment_approved = '1' ORDER BY comment_ID DESC LIMIT $no_comments"; $comments = $wpdb->get_results($request); $output = ''; if ($comments) { foreach ($comments as $comment) { $comment_author = stripslashes($comment->comment_author); if ($comment_author == "") $comment_author = "anonymous"; $comment_content = strip_tags($comment->comment_content); $comment_content = stripslashes($comment_content); $words=split(" ",$comment_content); $comment_excerpt = join(" ",array_slice($words,0,$comment_lenth)); $permalink = get_permalink($comment->ID)."#comment-".$comment->comment_ID; if ($comment_style == 1) { $post_title = stripslashes($comment->post_title); $url = $comment->comment_author_url; if (empty($url)) $output .= $before . $comment_author . ' on ' . $post_title . '.' . $after; else $output .= $before . "<a href='$url' rel='external nofollow'>$comment_author</a>" . ' on ' . $post_title . '.' . $after; } else { $output .= $before . '' . $comment_author . ': <a href="' . $permalink; $output .= '" title="View the entire comment by ' . $comment_author.'" rel="nofollow">' . $comment_excerpt.'</a>' . $after; } } $output = convert_smilies($output); } else { $output .= $before . "None found" . $after; } echo $output; } } Code (markup): I've edited the mdv_recent_comments function by adding nofollow to the code on lines 35 and 39. Then, every time you want to use the new function, use this block of code in place of what you posted originally: <?php if (function_exists('mdv_recent_comments_nofollow')) { ?> <div class="Cols" style="margin:0px 30px"> <h3>Recent Comments</h3> <ul> <?mdv_recent_comments_nofollow('10'); ?> </ul> </div> <?php } ?> Code (markup): Using a copy of the function should prevent many adverse side effects, but test it first, of course. Note that having multiple rel= attributes on an element would be invalid, but it is valid to have both external and nofollow inside a single attribute.