Stupid question but do you have to ovulate to get pregnant?

How does it work. I heard you won’t get pregnant within a week before your period but I just saw a post saying you can get pregnant anytime? Let’s say you ovulate in the middle of the month and then have sex 4 days before period can you get pregnant the time you have sex 4 days before your period?