I want to add a new text only if category have description. How I can check in phtml if category description contain text?

  • On which page you are trying to check this condition? Nov 2 '17 at 19:33
  • Magento_Catalog/templates/category/description.phtml
    – Robert
    Nov 2 '17 at 19:41
  • You want to add new text after category description or before ? Nov 2 '17 at 19:43
  • I think will be before description
    – Robert
    Nov 2 '17 at 19:45

In the Magento_Catalog/templates/category/description.phtml there is already an if clause which checks if a category has a description so what is the problem? Just add new code inside of the template.

  • You have right, thank you very much, I really don't see it :)
    – Robert
    Nov 2 '17 at 19:49

This if statement is already checking for category description.

<?php if ($_description = $block->getCurrentCategory()->getDescription()): ?>
    <div class="category-description">
        // Add you new text here...
        <?php echo "New TEXT"; ?>
        <?php /* @escapeNotVerified */ echo $this->helper('Magento\Catalog\Helper\Output')->categoryAttribute($block->getCurrentCategory(), $_description, 'description') ?>
<?php endif; ?>

Anywhere in custom phtml file :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$currentCategory = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category information

echo $currentCategory->getDescription();


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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