Try `$cat_name = $category->slug` instead of `$cat_name = $category->name`.

You are also missing `wp_reset_postdata();` just after `endwhile;`.