Remove content links (internal and external), but exclude post at specific categories

To exclude certain categories from the filter function that removes links from the post content, you can modify the function to check the categories of the post before removing the links.

Here is an example of how you can modify the function to exclude certain categories:

add_filter('the_content', 'removelink_content',1);

function removelink_content($content="") {
  // Get the categories of the current post
  $categories = get_the_category();
  
  // Set up an array of excluded category IDs
  $excluded_categories = array(2, 5, 8);
  
  // Check if any of the categories of the current post are in the excluded categories array
  $excluded = false;
  foreach ( $categories as $category ) {
    if ( in_array( $category->term_id, $excluded_categories ) ) {
      $excluded = true;
      break;
    }
  }
  
  // If the post is not in an excluded category, remove the links from the content
  if ( ! $excluded ) {
    preg_match_all("#<a(.*?)>(.*?)</a>#i",$content, $matches);
    $num = count($matches[0]);
    for($i = 0;$i < $num;$i++){
      $content = str_replace($matches[0][$i] , $matches[2][$i] , $content);
    }
  }
  
  return $content;
}

In this example, the function first gets the categories of the current post using the get_the_category() function. It then sets up an array of excluded category IDs (in this case, the IDs 2, 5, and 8). The function then checks if any of the categories of the current post are in the excluded categories array using the in_array() function. If the post is in an excluded category, the $excluded variable is set to true.

If the $excluded variable is not set to true, the function proceeds to remove the links from the content as before. If the $excluded variable is set to true, the function does nothing and returns the content as-is.

I hope this helps! Let me know if you have any questions.