cancel
Showing results for 
Search instead for 
Did you mean: 

how to edit magento 2 home page title (head title) programmatically ?

how to edit magento 2 home page title (head title) programmatically ?

hi guys,

                how can i edit magento 2 home page  title (head title) programmatically .

guide me ?

5 REPLIES 5

Re: how to edit magento 2 home page title (head title) programmatically ?

Hi @bharath553

 

Please try the below code to change the cms page title

 

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cmsPageFactory = $objectManager->create('Magento\Cms\Model\PageFactory');
$homePage = $cmsPageFactory->create()->load('Page Id or Url ');
$homePage->setTitle("Changes Title")->save();

 

Problem solved? Click Kudos & Accept as Solution!

 

Thanks

 

if issue solved,Click Kudos & Accept as Solution
Thanks
SelvaKumar

Re: how to edit magento 2 home page title (head title) programmatically ?

Please try this,

 

1. From controller

/** @var \Magento\Framework\View\Result\Page $resultPage */
$resultPage = $this->resultPageFactory->create();
$resultPage->getConfig()->getTitle()->set('Something');

2. From layout module_controller_action.xml

<?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>Something..</title>
    </head>
</page>

Re: how to edit magento 2 home page title (head title) programmatically ?

You need to do below in your controller file,

public function execute()
{
	$resultPage = $this->resultPageFactory->create();

        $pageMainTitle = $resultPage->getLayout()->getBlock('page.main.title');
        if ($pageMainTitle) {
            $pageMainTitle->setPageTitle(__('My Homepage'));
        }
        return $resultPage;
}
If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: how to edit magento 2 home page title (head title) programmatically ?

NO , i just want to edit the title in code. where can i find that code ?

Re: how to edit magento 2 home page title (head title) programmatically ?

Hi,

Please check the below configuration changes.


Go to the Admin panel > Content > Pages > Choose the Homepage > In the field "Page Title" add a new title you want to show > click on Save button.
or
Go to Admin Panel > Go to System > Configuration > Choose Design, open HTML Head tab > Enter your title in "Default title" field > Save Config

 

Hope it helps!