1

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') {
...
}
?>
2
  • You want your custom field's value from category in title.phtml file ? Nov 15, 2019 at 15:52
  • Yes, I want to replace the H1 title with the value in the attribute field.
    – Aknot
    Nov 15, 2019 at 15:57

1 Answer 1

1

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!

4
  • Let me know if this works for you @Alldo Nov 16, 2019 at 7:03
  • 1
    Thanks for your reply, ran into some problem with the attribute I want to test, they didn't become saved store view wise, need to fix that before i can test your solution :-) magento.stackexchange.com/questions/295865/…
    – Aknot
    Nov 16, 2019 at 12:09
  • 1
    Works perfect, thank you @Kishan Savaliya
    – Aknot
    Nov 17, 2019 at 7:23
  • You're welcome Buddy. :) Nov 17, 2019 at 7:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.