How to Set Page Title in Magento 2 Using Controller & XML
It is displayed on the top of the browser window and helps the visitors to identify the page among multiple tabs! 💫
Unfortunately, Magento 2 custom modules take the page address and show them as page titles. That can be pretty annoying for the visitors and can make your store look unprofessional. Well, there are ways to set page titles in Magento 2 — offering a smooth and pleasant customer experience. 😃
Recently, I stumbled upon a similar issue while working on one of my client’s website. In this blog post, I am going to share the methods that I used to set page title in Magento 2 using controller & XML. 😇
Table of Contents
Let’s go…!
Methods to Set Page Title in Magento 2
Use any of the two ways described below to change the page title in Magento 2.
Method #1: Set Page Title in Magento 2 Using Controller
Use this code to set Magento 2 page title using the controller:
1 2 3 |
/** @var \Magento\Framework\View\Result\Page $resultPage */ $resultPage = $this->resultPageFactory->create(); $resultPage->getConfig()->getTitle()->set('Enter the custom page title here'); |
Method #2: Set Page Title in Magento 2 Using XML
Use this code in the layout module_controller_action.xml file to set page title in Magento 2:
1 2 3 4 5 6 |
<php?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <head> <title>Enter the custom page title here</title> </head> </page> |
That’s it!
I hope now it’ll be easier for you to set the page title in Magento 2 using controller & XML.
Conclusion
Setting informative page titles is essential for any website as they help the users easily identify the pages while surfing multiple sites at once. If you are a Magento 2 developer, you can use the methods provided above to set the page title in Magento 2 using controller & XML. 💡
If you still have any doubts or queries, feel free to comment. 😇
I’d be happy to help!
Contribute to the community by sharing this Magento 2 tutorial through social media & online forums. (Sharing = Caring! 🙂
Thanks for reading! 🍀
Jignesh Parmar
An expert in his field, Jignesh is the team leader at Meetanshi and a certified Magento developer. His passion for Magento has inspired others in the team too. Apart from work, he is a cricket lover.
Prev
6 Experts Share Their Opinion On ‘Will ChatGPT Replace Developers?’
4 Quick Ways to Fix Shopify SSL unavailable Error
Next