Magento version: 2.0.0
How to check the current page is home page in the template file? There has a global function to do it?
Set block content with custom function :
protected $_request;
public function __construct(
...
\Magento\Framework\App\Request\Http $request,
...
) {
...
$this->_request = $request;
...
}
public function isHomepage()
{
if ($this->_request->getFullActionName() == 'cms_index_index') {
return true;
}
return false;
}
Template code :
<?php $isHome = $block->isHomepage(); ?>
Try:
$currentUrl = $this->getUrl('', ['_current' => true]);
$urlRewrite = $this->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);
$isHome = $currentUrl == $urlRewrite;
In addition you can use the \Magento\Theme\Block\Html\Header\Logo::isHomePage()
method if you has this block in all pages.
You can also check
$objectManagerCms = \Magento\Framework\App\ObjectManager::getInstance();
$cmsPage = $objectManagerCms->get('\Magento\Cms\Model\Page');
echo $cmsPage->getIdentifier(); //Current CMS Page Identifier