Same sidebar in all posts as in the category

Retrieving category post is assigned to is rather straightforward with get_the_category(). However note that post can be easily placed into multiple categories and you will need some (quite elaborate) logic if you want to apply any particular sorting logic to it.

From looking at your code however it’s not clear how exactly category/post/sidebar are tied to each other since you are retreiving sidebar info from post alone.