OAKLAND, Calif. — They had not come all the way back from an early ditch that J.A. Happ and Chad Green had lowered them into.

Yet the Yankees had runners at first and third with one out in the eighth inning and trailing by two runs Wednesday evening against A’s, and DJ LeMahieu and Aaron Judge ready to hit.

Thanks to a Mike Ford homer and Didi Gregorius’ RBI double in the seventh, the Yankees had a big shot in the eighth with the best hitter in the AL coming to the plate and Judge, who had shown hints in the past three games of emerging from a long slump.

So what happened?

Liam Hendriks, a gas-throwing right-hander, struck out LeMahieu with an off speed pitch, and after hitting triple digits with a fastball that Judge fouled straight back, Hendriks fanned the slugger with a breaking ball.

Hendriks finished the Yankees off in the ninth to preserve a 6-4 A’s win that was witnessed by 22,017 at the collapsing Coliseum.

“I thought a lot of good at-bats. Couldn’t quite break through to make it a real dynamic offensive night,” Aaron Boone said following the Yankees’ third straight loss. “Eleven hits again, so there are a lot of things happening good up and down the order.

“Just not that big hit tonight to really break through or have that huge inning. I thought our at-bat quality was really good, especially the second half of the game.’’

The loss, combined with the second-place Rays winning, shaved the Yankees’ lead in the AL East to nine lengths. The Dodgers winning moved them one game up on the Yankees for the best record in baseball.

The Yankees, who have lost three straight for the first time since July 25-27 to the Red Sox in Fenway Park, attempt to avoid opening a nine-game West Coast trip with three consecutive losses on Thursday.

Giving up home runs has hurt Happ for a good chunk of the season. He has served a career-high 31, which is tied for second among AL pitchers, and Wednesday night wasn’t any different.

Staked to a 1-0 lead by Gregorius’ leadoff double in the second followed by a run-scoring single by Mike Tauchman (3-for-3; two RBIs), Happ gave up a two-run homer to Khris Davis in the second and another two-run poke to Marcus Semien in the third. Green did well to allow just one run after inheriting a bases-loaded, no-out situation from Happ in the fifth, but Stephen Piscotty opened the sixth with a homer.

“Struggling to keep the ball in the park for whatever reason,’’ said Happ, who gave up five runs, four hits, walked two, hit a batter and dropped to 10-8. “They got two [homers] and had guys on base each time and that makes it tough.’’

So, too, does playing from behind, which the Yankees did from the second inning on and for the second straight game. The Yankees entered the game hitless in the previous seven at-bats with runners in scoring position but finished 3-for-9.

When the Yankees got done pounding the hopeless Orioles for four straight wins last week at home, the immediate schedule had more teeth. They split four with the Indians before leaving for three against the A’s, three at Dodger Stadium this weekend and three in Seattle next week.

But first there is the chore of not getting swept by the A’s, who are serious AL wild-card contenders and avoid having to break a losing streak against the Dodgers at Dodger Stadium.