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:
- Add this code to your theme’s functions.php file or in a site-specific plugin
- 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!
How to add more categories?
You could change line 3 to include an OR clause:
if( in_category(1) || in_category(2)) { // Etc.
How to add nofollow attribute just to specific link?