It’s called unsupervised pretraining, and they do it one layer at the time. No one does it like that anymore, because you get the same or better accuracy by training the same network from scratch (random initialization).
If you still insist on doing it like that (why?), then the code from the link I posted above is sufficient. Just remove upper layers, then add them back one by one.
Seems like you’re a beginner, so I suggest you take an online course first (fast.ai is pretty good).