KANSAS CITY, Mo. — The reigning World Series champions had been rendered an afterthought as the calendar flipped to August, the Kansas City Royals so far out of playoff contention that some were wondering whether they might begin jettisoning their stars and rebuilding.

They proceeded to have one of the best months in franchise history.

Despite back-to-back extra-inning losses to the Yankees, the Royals still finished 20-9 in August to climb back into the postseason chase. They’ve won seven of their past eight series and 18 of their past 24 games as they head into an important series against the Detroit Tigers this weekend.

The Tigers are among the teams standing between Kansas City and an AL wild-card spot.

“It really shows the character and how resilient they are,” said Royals outfielder Billy Burns, whose arrival in late July coincided with their recent run. “I saw it two years ago in the wild-card game when I was with Oakland, and we had them down with Jon Lester on the mound.

“Nothing with this team really surprises me.”

Still, the Royals went just 7-19 in July. Their starting rotation was atrocious, they had one of the most impotent lineups in the majors and a series of injuries had scuttled any sort of chemistry.

There was nothing to indicate that such an about-face was about to happen.

Then the praying mantis showed up.

On a team that believes wholeheartedly in karma, the arrival of the bug-eyed insect during a win over Toronto on Aug. 6 came with deeper meaning. The Royals adopted the Rally Mantis and took him on the road, where they won five of six games. And when the bug died in Minnesota — the Royals lost that day, by the way — they were unsurprised when another mantis flew in during a game in Detroit.

This time, Rally Mantis Jr. was given a proper cage complete with food, greenery and, well, insect versions of all the trappings that major league ballplayers have in their clubhouses.

Burns is the caretaker of the bug. And since his arrival, the postseason charge has pressed on. The Royals’ win total in August was their best since 1989, when they won 21 games.

“That shows you the makeup of this club,” said reliever Luke Hochevar, one of the longest-tenured Royals. “When you have really, really good talent combined with great makeup, that’s the recipe.”

The Royals have done it despite the injury trouble they steered clear of last year.

They lost right-hander Kris Medlen, one of their projected starters, to rotator cuff inflammation in May. Then they lost third baseman Mike Moustakas to a torn ACL in a freak collision with left-fielder Alex Gordon, who wound up missing several weeks with an injured wrist.

Hochevar was shelved with season-ending surgery for symptoms of thoracic outlet syndrome, which had caused pain and coldness in his pitching arm. All-Star closer Wade Davis followed him to the disable list with a flexor strain, though he is expected to rejoin the club this weekend.

Just in time for the final push for the playoffs.

Kansas City has appeared in the past two World Series, but in reality the once-downtrodden club has been playing meaningful baseball this time of year four straight years. The core group understands what it takes to be successful when the temperatures start to fall and the pressure starts to rise.

“We’re just taking each day as a new day,” first baseman Eric Hosmer said. “We’re not worrying about scoreboards or who is ahead of us in the standings. You know there’s so much time left and another team can get hot like we did. We just need to stay consistent.”

Much of the credit for the turnaround is owed to general manager Dayton Moore. He was hearing from plenty of folks in late July that it was time to sell off. The Royals were 11 games back in the AL Central and 9 1/2 games back in the wild-card race, and the window for contending appeared to have closed.

Moore instead decided to stand pat, confident that a bunch of scuffling guys could turn it around.

Now, the question is whether they did it in time.

“The experience of this team, we all realize that a lot of crazy stuff can happen,” Hosmer said. “We set out for the fact that it’s a 162-game season. So no matter what, if we’re having a good time or a bad time, we’re going to be ready for the next day.”