Multiple Loops
Outside the loop you can get available terms of the core taxonomy category with get_terms( 'taxonomy_name' )
. The resulting array contains objects like
object(stdClass)#141 (9) {
["term_id"] => string(1) "3"
["name"] => string(9) "The Name of your Category"
["slug"] => string(9) "name-of-tax-term"
["term_group"] => string(1) "0"
["term_taxonomy_id"] => string(1) "3"
["taxonomy"] => string(11) "slug_of_tax"
["description"] => string(41) "Description of Term."
["parent"] => string(1) "0"
["count"] => string(1) "3"
}
So you would want to get the category slug, not its name:
$cats = get_terms( 'category' );
You could then loop the categories like
foreach ( $cats as $cat ) {
$args = array(
'category_name' => $cat->slug,
'paged' => $paged,
'posts_per_page' => 2
);
// the loop
wp_reset_postdata();
}
One Loop
If your loop runs on the unchanged main query, the current category is already present in the query.
Just change
$args = array(
'category_name' => $cat_name,
'paged' => $paged,
'posts_per_page' => 2
);
to
$args = array(
'paged' => $paged,
'posts_per_page' => 2
);
in your code.
Besides, when not using get_the_category()
inside the loop, you have to pass a post ID as argument.