Or you could simply use the
if (!is_attachment())
check before doing anything.