Disable Attachment Page Except for Category

The problem is that is_category does not do what you thought it does. is_category( 'keep' ) tests that the current page is a category archive for the category keep, it does not check if the current attachment/post is in the keep category.

The official WordPress developers docs for is_category start with:

Determines whether the query is for an existing category archive page.


Instead, what you want is to check if the current post has a category assigned, aka has_category:

has_category( string|int|array $category = '', int|WP_Post $post = null ): bool

Checks if the current post has any of given category.