Question

I have found a lot of examples on how to create an extra attribute for the categories. I want to create a text field for adding a different H1 - instead of using the category name as the headings, on the category pages.

But how can I fetch the attribute from the title.phtml file?

(\app\design\frontend\Vendor\Theme\Magento_Theme\templates\html\title.phtml)

I have this to check if on a category page in title.phtml:

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$request = $objectManager->get('Magento\Framework\App\Action\Context')->getRequest();
/*
 * Check if on product page
 * ------------------------
 */
if ($request->getFullActionName() == 'catalog_category_view') {
...
}
?>
Was it helpful?

Solution

You can use below code

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$request = $objectManager->get('Magento\Framework\App\Action\Context')->getRequest();
/*
 * Check if on product page
 * ------------------------
 */
if ($request->getFullActionName() == 'catalog_category_view') {
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
    $customAttributeValue = $category->getData('custom_attribute'); //here you can replace your attribute code with 'custom_attribute'.
}
?>

Hope this will help you!

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top