On a celebrity gossip show here, the hosts recently projected the Instagram profiles of Turkish actors and pop stars on the screen and berated those who had not yet posted support for Turkey’s military offensive in Syria.

“There are those celebrities who even post when they go to the bathroom,” one host quipped. “They could just post a little thing saying, ‘I prayed and would pray again for the soldiers.’ ”

Disdainfully, the hosts flashed through more accounts, calling out those they faulted for ignoring the operation, which seeks to oust Kurdish fighters officially portrayed as terrorists.

Since the offensive began last month in the Afrin enclave just over the border, Turkish media and public opinion have rallied around the government, stirring nationalist sentiment and giving a boost to President Recep Tayyip Erdogan, who is up for reelection by next year.

On newsstands, headlines have blared support, and imams in mosques throughout the country have prayed for a military victory. Authorities have detained hundreds of people — including doctors and writers — for criticizing the assault. Even opposition leaders have fallen in line.

Erdogan has deftly framed the Afrin battle as an extension of Turkey’s war with Kurdish insurgents at home, tapping into years of public anger with the militant Kurdistan Workers’ Party, or PKK, which is linked to the Kurdish fighters in northern Syria.

In waging what has been dubbed “Operation Olive Branch,” he is also exploiting a surge of anti-Americanism among Turks, arising in part from U.S. military support for the Syrian Kurds. The Syrian Kurdish militia, called the YPG, or People’s Protection Units, has been a crucial American ally in the campaign to oust the Islamic State from Syria.

Since the operation began, public opinion polls have shown a large majority of Turks supporting the offensive. One poll, conducted by Turkey’s MAK research company and cited widely by pro-government media, said 82 percent of respondents believed that the operation will succeed. Ninety percent said the United States is “behind” the PKK and YPG.

That broad distrust of the United States comes at a time when, Turkish commentators say, relations between the two longtime allies have sunk to their lowest point since Turkey joined NATO in 1952.

During recent interviews in Istanbul, in working-class and upscale neighborhoods alike, Turks repeatedly questioned why the United States had joined forces with the YPG, which they see as inextricably linked to the PKK and thus to bomb attacks inside Turkey.

“I am not a government supporter,” said Yadagar Karakul, a 30-year-old event coordinator. But in Afrin, he said, the government “is protecting Turkey’s territory against terrorists on the border.”

“Nowhere in the world can you form an agreement with a terrorist organization,” he said, referring to the YPG.

At a teahouse in the conservative Tophane neighborhood in Istanbul, Ihsan Gumus, 39, defended the operation.

“Syria has become a problem for the entire world,” said Gumus, who is unemployed. “And if there is a terror organization on the border like the government says, then I support it.”

The Turkish offensive, including air and ground operations, aims to establish a buffer zone inside northern Syria enforced by Turkish-backed forces. The YPG was initially formed to protect Kurdish populations amid the chaos of the Syrian civil war.

Erdogan has said that after neutralizing Afrin, Turkey plans to extend its Syrian campaign eastward and oust the YPG fighters from the Kurdish-controlled city of Manbij. That could bring Turkish forces into direct conflict with the United States, which has an undisclosed number troops in that city.

In a phone call with Erdogan on Jan. 24, President Trump urged Turkey to “de-escalate, limit its military actions, and avoid civilian casualties,” according to a White House account. Gen. Joseph Votel, commander of the U.S. Central Command, told CNN recently that withdrawing from Manbij was “not something we’re looking into.”

Writing in the conservative daily newspaper Karar, which is sometimes critical of the president and his ruling party, columnist Mustafa Karaalioglu said the military campaign requires Turkish solidarity.

“With 11 days of the operation behind us,” he wrote, “almost the entire country shares the same feeling” about the offensive.

Another columnist, Ibrahim Karagul, writing in the pro-government Yeni Safak newspaper, minced few words in criticizing the United States.

“The U.S. is now the . . . greatest and most open threat for Turkey. It is an enemy country. The U.S. is the one that released the Kurdistan Workers’ Party (PKK) and Daesh on us,” Karagul wrote, using an Arabic term for the Islamic State. Our “ties with the U.S. have no meaning left.”

The enthusiasm for the offensive highlights how Turkish public opinion has evolved in recent years, from opposition to intervention in Syria’s war to support for what could be an open-ended operation.

At home, Turks also once backed government peace talks with the PKK — which has waged a decades-long struggle for Kurdish autonomy — resulting in a cease-fire from 2013 to 2015. Since then, however, Turkey has been roiled by myriad crises, hardening attitudes toward both Syria and the Kurds, who number about 15 million inside Turkey. There are another 2 million Kurds in Syria, where they now administer key stretches of territory.

Analysts say Erdogan is taking advantage of recent domestic instability, including renewed fighting with the PKK inside Turkey and a string of deadly attacks by the Islamic State, in hopes of turning public fears into votes next year.

Soner Cagaptay, author of “The New Sultan: Erdogan and the Crisis of Modern Turkey,” called the military campaign a “masterstroke” by the president.

“He’s mobilizing his base,” Cagaptay said. “The media has already decided that he has a victory.” And if Erdogan decides to call early elections, he said, “Afrin could help catapult him to a clear sweep at the polls.”

In mountainous Afrin, where the well-trained YPG has deep roots, Turkish forces could face stiff resistance. According to official figures, 13 Turkish soldiers have already been killed in the campaign. And the stakes could be even higher if Turkish forces move on Manbij.

“I don’t think Turkey should take this risk,” 60-year-old Arif Tulle, a hardware store owner in Istanbul, said of the possibility of clashing with U.S. forces.

“The U.S. and Turkey have been NATO allies for years,” he said. Still, “there are threats on our border. And I want to live in peace.”