Custom permalink structure for posts in certain categories

here is a solution to search through all categories, not only the first one.

add_filter('post_link', 'custom_permalink', 10, 2);
function custom_permalink( $permalink, $post ) {
    // Get the categories for the post
    $categories = get_the_terms($post->ID, 'category'); 
    if (!empty($categories)) {
        foreach ($categories as $category) {
            if ($category->cat_name == "ICO") {
                $cat_name = strtolower($category->cat_name);
                $permalink = trailingslashit( home_url("https://wordpress.stackexchange.com/" . $cat_name . "https://wordpress.stackexchange.com/" . $post->post_name . "https://wordpress.stackexchange.com/" ) );
                break; //we found ico, so exit foreach immediately
            }
        }
    }
    return $permalink;
}