cancel
Showing results for 
Search instead for 
Did you mean: 

Get Page Title In PHTML? CE 2.4

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

 

12 REPLIES 12

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!

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

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;

?>

Re: Get Page Title In PHTML? CE 2.4

Thanks for the inforamtion  epayitonline

Re: Get Page Title In PHTML? CE 2.4

Thank you so much for such a well-written article. It’s full of insightful information. Your point of view is the best among many without fail. For certain, It is one of the best Forum in my opinion.

epayitonline 

Re: Get Page Title In PHTML? CE 2.4

Get page title in Magento 2, At magento every phtml file have block class.So you get page title by $block->​getLayout()->getBlock('page.main.title')->getPageTitle(). At magento every phtml file have block class.So you get page title by $block->getLayout()->getBlock('page.main.title')->getPageTitle()

Get Page Title In PHTML? CE 2.4, Any help? Smiley Sad. Labels: CE 2.3 · community-edition · magento2. 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

How to get page title in magento?, use this code $this->getLayout()->getBlock('head')->getTitle();. you can use this code in your phtml file. oh dear, i just grep'ed for page.main.title and didn't realised it shows up in soo many places Smiley Surprised i initially just grep'ed for htmlClass="page-title-wrapper" and only saw title.phtml referenced from that thank you – roman p. aces etm

Re: Get Page Title In PHTML? CE 2.4

Keurig K525C is one of the best selling machines with a stylish design and above all, it’s affordable.

Re: Get Page Title In PHTML? CE 2.4

Want to watch or download a movie on Ganool, but confused looking for the link to the latest site address of the original Ganool.com

Re: Get Page Title In PHTML? CE 2.4

As I have checked your xml file and found that you have defined core block file i.e Magento\Framework\View\Element\Template, you need to replace your Block class to Custom Block file in your xml file then after you can call your Custom Block in your Custom Template File.

Please let me know if its helpful for you Smiley Happy

 

Thanks