The mockery and the spin from President Trump’s political opponents poured in as soon as he made his announcement on Sunday about US forces killing ISIS chief Abu Bakr al-Baghdadi.

The president didn’t restrain his urge to take credit for the triumph or hold back on describing the events in pure Trumpspeak. He spoke of Baghdadi “whimpering” and “dying like a dog.” While giving the proper credit to the heroism of the operators who put an end to the career of a murderer and rapist who was one of the planet’s true villains, Trump did the presidential equivalent of an NFL touchdown dance.

Yet as far as his opponents in the media were concerned, the president’s behavior was not only unseemly but also dishonest, since they think he deserves no credit for a moment that put a bow on the successful war the United States has been waging against ISIS on his watch.

That was the keynote of coverage of the Baghdadi operation in the New York Times, CNN and most other liberal outlets. Instead of allowing Trump to bask in the glory of this victory, reporters and pundits widely bashed the president for supposedly being clueless about the operation and the intelligence establishment, for pursuing policies that made the fight against ISIS harder and for not giving top Democrats a heads up about the impending attack.

They extended Trump none of the adulatory coverage President Barack Obama received in the wake of the 2011 killing of Osama bin Laden.

Why is the media more focused on party-pooping on Baghdadi, when they couldn’t stop cheering the bin Laden op?

The killing meant that for at least a day or two, the Democrats’ effort to impeach Trump by any means necessary didn’t dominate the news cycle. ­Instead of slanted commentary justifying twisting the president’s unseemly conversation with Ukraine’s leader into an impeachable crime, the country was reminded instead of one of Trump’s inarguable successes: the destruction of the ISIS caliphate that stubbornly resisted Obama’s half-hearted efforts.

As is the case with every other triumph of American arms, the true glory ­belongs to the courageous men and women on the ground who carried out this perilous task — and the intelligence analysts and other US personnel who made it possible.

Yet as was the case with Obama and bin Laden, Trump gave the final OK for the mission as commander-in-chief. He’s also the one who would have gotten the lion’s share of the blame had it gone wrong, even if failure had been caused by an accident of fate — as President Jimmy Carter learned when the attempt to rescue the US hostages in Iran flopped in April 1980.

That’s why Obama was entitled to boast about bin Laden’s killing even if his own endless touchdown dance was equally unseemly — turning the bravery of Navy SEALs into a partisan talking point throughout the 2012 election.

But however off-putting Trump’s bragging may seem, he actually deserves just as much if not more credit for Baghdadi than Obama did for bin Laden.

While Trump’s move earlier this month to pull troops out of Syria was misguided, the military defeat of ISIS that sent Baghdadi scurrying to the hole in which he died was the fruit of Trump’s decision-making.

Obama’s efforts against ISIS were hampered by the White House’s caution and micromanagement. Trump unleashed the US military. He loosened the rules of engagement and took a hands-off approach to the operations planned by the soldiers, Marines and airmen tasked with the job, resulting in the rout of the caliphate.

The test now for Trump is whether he learns from Obama’s mistakes after bin Laden’s death. The rise of ISIS was made possible because Obama acted as if that one operation ended the war on Islamist terror, as he and his surrogates claimed during the 2012 campaign. If Trump treats Baghdadi’s killing as the end of the fight rather than as just one among many battles to be fought and won, his Middle East legacy will be as flawed as that of Obama.

But for now, all those who winked or cheered when Obama used bin Laden as a campaign prop but who now denigrate Trump for treating Baghdadi’s deserved end as a personal triumph need to shut up.

Jonathan S. Tobin is editor-in-chief of JNS.org. Twitter: @JonathanS_Tobin