4

Is there any way to get current page title by store code or id?

The following code gives the current page title but is of currently selected store view eg http://example.com/it/shop.html which is Italian store view.

The context is ~/Model/Controller/Page.php

$title = Mage::getSingleton('core/layout')->getBlock('head')->getTitle();

I am trying to figure out way to get current page title by specific store code like en or it or id. This is for always forcing the Segment.io analytics page call to English text.

atulmy
  • 1,364
  • 15
  • 23

3 Answers3

1

Have you tried below code??

  $page = Mage::getModel('cms/page')->setStoreId(Mage::app()->getStore()->getId())->load('Page-Identifier');

    $pageTitle = $page->getTitle();
Vishwas Soni
  • 467
  • 10
  • 16
  • Nope, doesnt work. First, for this we need to pass the current page identifier. Second, it will work only in case of CMS pages if we find current page identifier and not for pages like product catalog and contact. – atulmy Jul 20 '15 at 13:03
  • 1
    If you need for catalog and product page then you should create the observer. This might help you -->http://magento.stackexchange.com/questions/71037/change-dynamic-title-of-review-page/71047#71047 – Vishwas Soni Jul 21 '15 at 04:51
  • Updated the question, please refer. – atulmy Jul 21 '15 at 09:54
0

In the context of a PHTML file I think you can only translate string like this:

$this->__($this->getLayout()->getBlock('head')->getTitle())

If you want to use store view translation you should setTitle somewhere in the block class or controller.

Clonkex
  • 3,373
  • 7
  • 38
  • 55
Shack Ro
  • 79
  • 2
  • The context is ~/Model/Controller/Page.php Current code: `$title = Mage::getSingleton('core/layout')->getBlock('head')->getTitle();` – atulmy Jul 21 '15 at 09:52
0

No, at least not that easy. The title could have been set anywhere, and it is set as localized string without any additional information.

Examples

Category page

The title is set within the Mage_Catalog_Block_Category_View block

if ($headBlock = $this->getLayout()->getBlock('head')) {
    $category = $this->getCurrentCategory();
    if ($title = $category->getMetaTitle()) {
        $headBlock->setTitle($title);
    }

Product page

The title is set within the Mage_Catalog_Block_Product_View block

$headBlock = $this->getLayout()->getBlock('head');
if ($headBlock) {
    $product = $this->getProduct();
    $title = $product->getMetaTitle();
    if ($title) {
        $headBlock->setTitle($title);
    }

CMS page

The title is set within the Mage_Cms_Block_Page block

$head = $this->getLayout()->getBlock('head');
if ($head) {
    $head->setTitle($page->getTitle());

Cart

The title is set within the Mage_Checkout_CartController (not a block this time!)

$this
    ->loadLayout()
    ->_initLayoutMessages('checkout/session')
    ->_initLayoutMessages('catalog/session')
    ->getLayout()->getBlock('head')->setTitle($this->__('Shopping Cart'));

and so on, and so on

Unfortunately there is no single point to hook in, you would have to handle all cases separately.

The only common method is Mage_Page_Block_Html_Head::setTitle() and as stated before, this is too late because it already gets the localized string.

Also, as you can see in the examples, sometimes the title is a translated text with __() and sometimes it is an attribute of a model in the context of the current store view.

Fabian Schmengler
  • 24,155
  • 9
  • 79
  • 111