You Yifan is actually the son of Hanwang, whose real name is Zhu. Zhu Zhanji doesn't know especially the identity of his cousin. Finally, Hanwang rebelled and wanted to seize the throne, so he let You Yifan stay with him. After the job is done, You Yifan will be the prince. So, Hanwang entered the palace directly, while Zhu Zhanji confronted the rebels in order to save his father's army, while Yao Ziheng settled the fleeing royal family.
Zhu Zhanji successfully became emperor and married Yao Ziliao. After Hu Shanxiang was abolished, Yao Ziliao was appointed as the successor, that is, the grandson in history.