On a day when President Trump vowed to tone down his rhetoric and help the country heal following two mass slayings, he did the opposite — lacing his visits Wednesday to El Paso and Dayton, Ohio, with a flurry of attacks on local leaders and memorializing his trips with grinning thumbs-up photos.

A traditional role for presidents has been to offer comfort and solace to all Americans at times of national tragedy, but the day provided a fresh testament to Trump’s limitations in striking notes of unity and empathy.

When Trump swooped into the grieving border city of El Paso to offer condolences following the massacre of Latinos allegedly by a white supremacist, some of the city’s elected leaders and thousands of its citizens declared the president unwelcome.

In his only public remarks during the trip, Trump lashed out at Sen. Sherrod Brown of Ohio and Dayton Mayor Nan Whaley, both Democrats, over their characterization of his visit with hospital patients in Dayton.

“We had an amazing day,” Trump said in El Paso as he concluded his visit. “As you know, we left Ohio. The love, the respect for the office of the presidency.”

Trump also praised El Paso police officers and other first responders and shook their hands, telling one female officer, “I saw you on television the other day and you were fantastic.”

None of the eight patients still being treated at University Medical Center in El Paso agreed to meet with Trump when he visited the hospital, UMC spokesman Ryan Mielke said. Two victims who already had been discharged returned to the hospital with family members to meet with the president.

“This is a very sensitive time in their lives,” Mielke said. “Some of them said they didn’t want to meet with the president. Some of them didn’t want any visitors.”

Before Trump’s visit Wednesday, however, some of the hospitalized victims accepted visits from a number of city and county elected officials, as well as Reps. Veronica Escobar (D-Tex.) and Jesús “Chuy” García (D-Ill.).

White House press secretary Stephanie Grisham said the president and first lady Melania Trump met with “victims of the tragedy while at the hospital” and were “received very warmly by not just victims and their families, but by the many members of medical staff who lined the hallways to meet them. It was a moving visit for all involved.”

El Paso and Dayton were not merely the latest in the multiplying series of American mass shootings. The carnage in El Paso is being investigated as an act of domestic terrorism, with parallels between a racist manifesto posted minutes before the shooting and the president’s own anti-immigration rhetoric.

This has thrust Trump into the center of a roiling political and societal debate, with some Democratic leaders saying the president has emboldened white supremacy and is a threat to the nation.

Former vice president Joe Biden, who is running to unseat Trump in 2020, said in a speech Wednesday, “We have a president with a toxic tongue who has publicly and unapologetically embraced a political strategy of hate, racism and division.”

Both in Dayton and El Paso, Trump kept almost entirely out of public view, a marked break with tradition, as presidents visiting grieving communities typically offer public condolences.

Trump avoided the Oregon district where the shooting in Dayton took place, and just a short drive from Miami Valley Hospital, which he did visit. Whaley said he would not have been welcome in the Oregon District, where scores of demonstrators congregated, holding ­anti-Trump signs and chanting “Do something!” in a call for stricter gun laws.

Brown and Whaley described the visit by the president and first lady in favorable terms.

“They were hurting. He was comforting. He did the right things. Melania did the right things,” Brown told reporters. “And it’s his job in part to comfort people. I’m glad he did it in those hospital rooms.”

Whaley added: “I think the victims and the first responders were grateful that the president of the United States came to Dayton.”

Both Brown and Whaley, however, were also sharply critical of Trump’s divisive rhetoric and Republican resistance to gun-control legislation.

Whaley later responded to Trump’s comments about her and Brown by calling him “a bully and a coward.” She said on CNN, “It’s fine that he wants to bully me and Sen. Brown. We’re okay. We can take it.”

The traveling press corps was not allowed to observe Trump’s visit with three victims who remained hospitalized. It fell therefore to White House aide Dan Scavino to proclaim in a tweet that Trump “was treated like a Rock Star inside the hospital.”

Trump and the first lady also met with police officers, fire officials, trauma surgeons and nurses at the facility, which treated 23 victims of the shooting. The hospital invited victims who had already been released to come back and meet with the president and the first lady.

“It was an authentic visit,” hospital president Mike Uhl said, praising Trump as “attentive, present and extremely accommodating.”

Trump offered his own affirmation on Twitter: “It was a warm & wonderful visit. Tremendous enthusiasm & even Love.”

Grisham said journalists were kept out of the hospital visit because staff did not want it to devolve into “a photo op” and overwhelm the victims with media.

The White House, however, distributed its own photos of Trump smiling for pictures with first responders, along with a slickly produced video, helping make the president the center of attention.

Trump’s reception in El Paso was less hospitable, and not only because so many local leaders have said they believe his rhetoric inspired Saturday’s slayings at a shopping center near the U.S.-Mexico border. Although he won the state of Texas in the 2016 election, Trump captured just 25.7 percent of the vote in El Paso County, the worst performance recorded here by a major-party presidential candidate in at least two decades.

An ever-growing makeshift memorial has sprouted near the shooting scene that features piles of colorful flowers, a row of white crosses, a line of prayer candles, as well as messages to the president. “Mr. T, Respect our sorrow and grief. Do not ‘invade’ our city,” reads one note, a reference to Trump’s repeated warnings of a migrant “invasion” at the border.

Just before Trump arrived in El Paso — where he and the first lady met with law enforcement personnel at an emergency operations center following their hospital visit — several hundred people gathered in opposition to his trip.

Congregating under the hot midday sun in a baseball field for an “El Paso Strong” event, some held homemade signs. “Go home! You are NOT welcome here!” read one. “This was Trump-inspired terrorism,” read another. “Trump repent,” read a third.

At one point, the crowd chanted, “Send him back!” — a nod to the incendiary “Send her back!” chant about Somali-born Rep. Ilhan Omar (D-Minn.) at one of Trump’s campaign rallies last month.

“We feel like right now we should be in mourning, and we feel like we should be collecting our thoughts, we should be doing vigils and we should be gathering together as a community. We believe it is an insult that the president is coming here,” said one of the organizers, Jaime Candelaria, a 37-year-old singer and songwriter.

Escobar said onstage, “In this moment, someone is visiting … I felt it was important that we come together and not focus on the visitor, but focus on El Paso.” She added, “We will not stop resisting the hate! Resisting the bigotry! Resisting the racism!”

During his flight home from El Paso, Trump attacked Rep. Joaquin Castro (D-Tex.), the twin brother of presidential candidate Julián Castro, tweeting that he “makes a fool of himself every time he opens his mouth.” The congressman has come under scrutiny for publicizing a list of San Antonio donors who have contributed to Trump and accusing them of “fueling a campaign of hate.”

On Saturday in El Paso, authorities said, a man opened fire inside the Walmart, killing 22 people and injuring two dozen others. At 1:05 a.m. Sunday, a gunman killed nine people and injured 27 others outside a bar in Dayton, police said.

All week, Trump has zigzagged between two competing instincts: unite and divide.

In the immediate aftermath of the shootings, Trump remained cloistered at his golf club in Bedminster, N.J., issuing only short statements on Twitter. Back at the White House on Monday, the president delivered a scripted speech in which he preached harmony.

“Now is the time to set destructive partisanship aside — so destructive — and find the courage to answer hatred with unity, devotion and love,” Trump said, reading from teleprompters.

The president did not heed his own advice, however. Late Tuesday night, he took to Twitter to attack Beto O’Rourke, the former El Paso congressman running for president who has said Trump bears some responsibility for the shooting there because of his demonization of Latino immigrants.

Trump tweeted: “Beto (phony name to indicate Hispanic heritage) O’Rourke, who is embarrassed by my last visit to the Great State of Texas, where I trounced him, and is now even more embarrassed by polling at 1% in the Democrat Primary, should respect the victims & law enforcement — & be quiet!”

Then, as he departed the White House on Wednesday morning en route to Ohio, Trump told reporters he would refrain from attacking his adversaries during the trip.

“I would like to stay out of the political fray,” the president said. Asked about his rhetoric, he said he thinks it “brings people together” and added, “I think we have toned it down.”

That detente lasted only a few minutes. Answering a reporter’s question about Biden, Trump pounced. “Joe is a pretty incompetent guy,” the president said. “Joe Biden has truly lost his fastball, that I can tell you.”

By the time the president had left Dayton, he was back on Twitter and sniping at Democrats, a tirade triggered by his consumption of cable television news aboard Air Force One.

“Watching Sleepy Joe Biden making a speech. Sooo Boring! The LameStream Media will die in the ratings and clicks with this guy,” the president wrote.

Then he lashed out at Brown and Whaley, falsely accusing them of “totally misrepresenting” the reception he received at Miami Valley Hospital. He alleged that their news conference immediately after the president’s visit “was a fraud.”

But neither Brown nor Whaley said Trump received a poor reception at the hospital.

When Whaley first saw Trump’s tweets criticizing her and Brown, she paused for a moment to read them on a cellphone and said, “I don’t — I mean, I’m really confused. We said he was treated, like, very well. So, I don’t know why they’re talking about ‘misrepresenting.’

“Oh, well, you know,” the mayor added with a shrug. “He lives in his world of Twitter.”

Correction: A previous version of this story included a reference to Shawn Nixon, 20, who identified himself Wednesday as an employee who was in an El Paso Walmart store when it was attacked over the weekend by a shooter who killed 22 people. Walmart said in a statement Thursday that he “was not an associate at store 2201 at the time of the shooting.” Nixon also acknowledged in an interview Thursday that he was not a Walmart employee, but said he was in the store during the attack. The reference and comments from Nixon have been removed from the story.

Parker and Johnson reported from El Paso, and Rucker and Sonmez reported from Washington. Arelis R. Hernández in Dayton, Robert Moore in El Paso, and Colby Itkowitz and John Wagner in Washington contributed to this report.