X

How To Add the nofollow Attribute to Category Specific Links

Snippets by IsItWP

Are you looking for a way to add the nofollow attribute to category specific links? This snippet will add the rel="nofollow" attribute to links to the specified category within the post content.

Instructions:

  1. Add this code to your theme’s functions.php file or in a site-specific plugin
  2. Change the category id in line 3 to the category to which you want to add the nofollow attribute.
function nofollow_cat_posts($text) {
global $post;
        if( in_category(1) ) { // SET CATEGORY ID HERE
                $text = stripslashes(wp_rel_nofollow($text));
        }
        return $text;
}
add_filter('the_content', 'nofollow_cat_posts');

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 9 Best SEO Tools to Grow Your Website Traffic, FAST!

Comments   Leave a Reply

  1. How to add more categories?

    1. You could change line 3 to include an OR clause:

      if( in_category(1) || in_category(2)) { // Etc.

  2. How to add nofollow attribute just to specific link?

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!