0

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

4
  • 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
1

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.

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

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') ?>
    </div>
<?php endif; ?>
0

Anywhere in custom phtml file :

<?php
$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.