cancel
Showing results for 
Search instead for 
Did you mean: 

Get Page Title In PHTML? CE 2.4

Highlighted

Get Page Title In PHTML? CE 2.4

Hiya, I'm struggling with something simple.

I'm trying to print the Page Title in the custom block from my custom.phtml and I'm getting nothing.

I've removed the Magento original page title block from all pages by updating default.xml

<referenceContainer name="columns.top">
<referenceBlock name="page.main.title" remove="true"/> </referenceContainer>

I've created my own block and pointed it to my phtml file in the same default.xml

<referenceContainer name="page.wrapper">
    <container name="page_title_container" as="page_title" label="Page Title" before="under_banner_bar_container" htmlTag="div">
        <block class="Magento\Framework\View\Element\Template" name="page_title" template="Magento_Theme::page_title.phtml"/>
    </container>
</referenceContainer>

Now the problem is even if i copy paste the contents of original /module-theme/view/frontend/templates/html/title.phtml

into my custom page_title.phtml I'm getting nothing, 0. Smiley Sad
It's not the case of caching or anything similar as any other type of content gets displayed no problem.

All I'm looking for is a way to access that $titleHtml variable from title.phtml somehow...
Any help? Smiley Sad

 

4 REPLIES 4
Highlighted

Re: Get Page Title In PHTML? CE 2.4

Hi @roman_polevecko 

 

At magento every phtml file have block class.So you get page title by

 

 $block->getLayout()->getBlock('page.main.title')->getPageTitle()
 
It may help you!
Problem Solved ? Please click on 'Kudos' & Accept as Solution!
Highlighted

Re: Get Page Title In PHTML? CE 2.4

Thanks for the update and quick reply. I'll be sure to keep an eye on this thread. Looking for the same issue. Bumped into your thread. Thanks for creating it. Looking forward for solution 

aces etm

Highlighted

Re: Get Page Title In PHTML? CE 2.4

Heya. I couldn't figure the proper way of doing it so I resorted to using an object manager.
It's easy but goes against magento coding standards.

<?php

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$config        = $objectManager->get('\Magento\Theme\Block\Html\Title');
$heading       = $config->getPageHeading();

echo $heading;

?>
Highlighted

Re: Get Page Title In PHTML? CE 2.4

Thanks for the inforamtion  epayitonline