The Portuguese freebooter Gerald the Fearless has by 1168 been so successful that his eastward expansion threatens the southward expansion of the Kingdom of León.
These actions are in violation of the succession arrangements laid down by Alfonso VII at Sahagún, since they comprise lands whose conquest had been assigned to León.
A few of Gerald's conquests in the far east had even been assigned to Castile.
The Leonese king, Ferdinand II, son of Alfonso VII, had taken action immediately after the taking of Cáceres, probably early in the spring of 1166, capturing Alcántara later that year and thus securing a crossing over the Tagus.
Subsequently, he has allied with the Almohad caliph Yusuf I, who has warned him of Gerald and the Portuguese's encroachments on his interests.
Gerald takes the taifa and city of Badajoz in the early summer of 1169 after a long siege, but the garrison takes refuge in the citadel, the alcazaba, the siege of which continues.
Seeing an opportunity to add to his domains the chief city of the region at the expense of both his Muslim and Christian enemies, Afonso I of Portugal arrives with an army to Badajoz to relieve his nominal vassal.
This provokes the opposition of Ferdinand of León, who claims Badajoz as his own and comes south with an army at the request of Yusuf, who has already sent a contingent of five hundred cavalry to assist the garrison.
The besieging Portuguese are themselves besieged by the Leonese and fighting break out in the streets.
While trying to flee, Afonso is caught on the hinge of a gate and flung from his horse, breaking his leg.
He is captured by Ferdinand's men, while Gerald is captured by the Leonese majordomo, Fernán Ruiz de Castro, called el Castellano ("the Castilian").
He is an important person at court, who for a while holds the highest military post in the capital (tenente turris Legionis, "possessing the tower of León").
He is the king's brother-in-law, being married to Stephanie the Unfortunate, an illegitimate daughter of Alfonso VII by his second mistress, Urraca Fernández, and thus a half-sister of Ferdinand II.
After the mêlée, the Leonese have control of the town and the alcazaba, which they soon relinquish to their Muslim allies.
Ferdinand succeeds in gaining the valley of the upper Limia and the regions of Toroño (around Tuy), Capraria (around Verín), and Lobarzana (around Chaves) from Afonso in exchange for his release.
Several of Gerald's conquests are ceded to purchase his freedom.
Ferdinand retains Cáceres, but …