Only show current category post

Use the function get_the_category(); inside the WordPress loop and pass the get_the_ID() function which will get the current post ID in the loop.

get_the_ID() will return the post id and whereas the the_id(); will just display.


if( $the_query->have_posts() ) : while( $the_query->have_posts() ) :  $the_query->the_post();

     $category_array = get_the_category( get_the_ID() );

     print_r( $category_array );

  endwhile; 

 endif; 

wp_reset_postdata();

Hopefully, the above code will solve the issue.