It was a foundational promise of Donald Trump’s historic presidential campaign: Mexico would pay for his 2,000-mile border wall. But as he desperately fights for $5.7 billion in taxpayer money for the project, Trump now claims he never said Mexico would directly foot the bill.

“Obviously, I never said this and I never meant they’re going to write out a check,” the president told reporters Thursday at the White House.

He did say it ­— at least 212 times during his campaign and dozens more since he took office. And he put it in writing — in a March 2016 memo to news outlets and then posted to his campaign website.

Specifically, Trump threatened to cut off billions of dollars in remittance payments from Mexican nationals in the United States to families in their home country. That, he proclaimed, would pressure the Mexican government to cough up “a one-time payment of $5-10 billion” for the wall.

Experts said at the time that the plan would not work, and the Trump administration never tried to enact it. But 2 ½ years later, with parts of the federal government shut down for three weeks in a budget impasse over Trump’s wall, the episode illustrates how his routine application of falsehoods, exaggerations and lies in service of political combat has come back to burn him.

First, former Mexican President Enrique Peña Nieto openly defied Trump and canceled two scheduled visits to the White House, one in 2017 and one in 2018, in retaliation for Trump’s demands that Mexico pay.

“Mexico will not pay for any wall,” he stated. His successor, Andres Manuel López Obrador, has shown no willingness to change course.

The Republicans who controlled Congress over the past two years never made funding the wall with taxpayer dollars a priority.

And now during the shutdown, the White House is searching far and wide for potential pots of money it could tap as the president considers declaring the border a national emergency — a move that would be sure to kick off a legal battle and inflame political tensions.

“The story keeps changing by the day ­— like everything,” said Cecilia Munoz, a vice president at New America, a liberal think tank, who served as White House domestic policy adviser under President Barack Obama. Of Trump’s original plan for funding the wall, she added: “They had no earthly idea how they would get Mexico to do that so they came up with an idea to try to pass the laugh test, which they didn’t do.”

Trump and his aides have floated other ideas to pressure Mexico to pay — canceling visas or increasing processing fees for Mexicans and taxing imported goods at 20 percent. But the Mexican government has flatly refused.

Most recently, Trump has resorted to arguing that Mexico will indirectly pay through a revised trade deal his administration signed with Mexico and Canada.

But that deal has yet to be ratified by Congress, contains no provision earmarking money for the wall, and economists have doubted whether it would significantly increase revenue to the U.S. Treasury.

“Obviously, they’re not going to write a check,” Trump said of Mexico on Thursday. “But they are paying for the wall indirectly, many, many times over, by the really great trade deal we just made.”

News fact-checkers have poked holes in Trump’s assertions. And Democrats have not been swayed, confident that the president is struggling to convince the public that his strategy to shut down the government for a publicly funded border wall is a political loser.

“Today is Thursday. That means @realDonaldTrump is lying, again,” Rep. Ted Lieu, D-Calif., wrote on Twitter, referring to Trump’s claim about Mexico. “Hard for Democrats to negotiate with POTUS when he makes stuff up, changes his mind on a whim and lies repeatedly.”

Trump has been promising that Mexico would pay for a wall since before he was a candidate for the White House, and the vow figured prominently in his June 16, 2015, campaign announcement.

“I will build a great wall,” he declared that day at Trump Tower in New York. “And I will have Mexico pay for that wall. Mark my words.”

What might have seemed like a preposterous boast from a vanity candidate became a staple of his campaign rallies, where supporters chanted, “Build the wall!” Trump would often add: “Who’s going to pay for the wall?” The crowd would respond: “Mexico!”