Stack Exchange network consists of 180 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Try $cat_name = $category->slug instead of $cat_name = $category->name.
$cat_name = $category->slug
$cat_name = $category->name
You are also missing wp_reset_postdata(); just after endwhile;.