cancel
Showing results for 
Search instead for 
Did you mean: 

Get Current Category

Get Current Category

Hi Newbie here to M2

 

What is the correct way to implement $getCurrentCategory->getName(); 

in left.phtml where it says __(Shop By)

 

 
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/**
 * Category left navigation
 *
 * @var $block \Magento\Catalog\Block\Navigation
 */
?>
<?php if (!$block->getCategory()) {
    return;
} ?>
<?php $_categories = $block->getCurrentChildCategories(); ?>
<?php $_count = is_array($_categories) ? count($_categories) : $_categories->count(); ?>
<?php if ($_count): ?>
    <div class="block filter">
        <div class="title">
            <strong><?= /* @escapeNotVerified */ __('Shop By') ?> NEED CATEGORY NAME HERE</strong>
        </div>
        <div class="content">
            <strong class="subtitle"><?= /* @escapeNotVerified */ __('Shopping Options') ?></strong>
            <dl class="options" id="narrow-by-list2">
                <dt><?= /* @escapeNotVerified */ __('Category') ?></dt>
                <dd>
                    <ol class="items">
                        <?php /** @var \Magento\Catalog\Model\Category $_category */ ?>
                        <?php foreach ($_categories as $_category): ?>
                            <?php if ($_category->getIsActive()): ?>
                                <li class="item">
                                    <a href="<?= /* @escapeNotVerified */ $block->getCategoryUrl($_category) ?>"<?php if ($block->isCategoryActive($_category)): ?> class="current"<?php endif; ?>><?= $block->escapeHtml($_category->getName()) ?></a>
                                </li>
                            <?php endif; ?>
                        <?php endforeach ?>
                    </ol>
                </dd>
            </dl>
        </div>
    </div>
<?php endif; ?>
 

 

Cheers

 

Mal

3 REPLIES 3

Re: Get Current Category

Hello @mallongstoe6b2 

 

To display the current category name in the code provided, you can use the $block->getCurrentCategory()->getName() method. Here's how you can modify the code:

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/**
 * Category left navigation
 *
 * @var $block \Magento\Catalog\Block\Navigation
 */
?>
<?php if (!$block->getCategory()) {
    return;
} ?>
<?php $_categories = $block->getCurrentChildCategories(); ?>
<?php $_count = is_array($_categories) ? count($_categories) : $_categories->count(); ?>
<?php if ($_count): ?>
    <div class="block filter">
        <div class="title">
            <strong><?= /* @escapeNotVerified */ __('Shop By') ?> <?= $block->getCurrentCategory()->getName() ?></strong>
        </div>
        <div class="content">
            <strong class="subtitle"><?= /* @escapeNotVerified */ __('Shopping Options') ?></strong>
            <dl class="options" id="narrow-by-list2">
                <dt><?= /* @escapeNotVerified */ __('Category') ?></dt>
                <dd>
                    <ol class="items">
                        <?php /** @var \Magento\Catalog\Model\Category $_category */ ?>
                        <?php foreach ($_categories as $_category): ?>
                            <?php if ($_category->getIsActive()): ?>
                                <li class="item">
                                    <a href="<?= /* @escapeNotVerified */ $block->getCategoryUrl($_category) ?>"<?php if ($block->isCategoryActive($_category)): ?> class="current"<?php endif; ?>><?= $block->escapeHtml($_category->getName()) ?></a>
                                </li>
                            <?php endif; ?>
                        <?php endforeach ?>
                    </ol>
                </dd>
            </dl>
        </div>
    </div>
<?php endif; ?>

By adding <?= $block->getCurrentCategory()->getName() ?> in the <strong> tag, you can display the current category name dynamically

 

Was my answer helpful? You can accept it as a solution.
175+ Professional Extensions for M1 & M2
Need a developer?Just visit Contact Us Now

Re: Get Current Category

Which one of the following, would be regarded as good practice, when implementing this
or doesn't it matter?

Cheers

Mal

 

<?= /* @escapeNotVerified */ __('Shop By') ?><?= /* @escapeNotVerified */ $block->getCurrentCategory()->getName() ?>

<?= /* @escapeNotVerified */ __('Shop By') /* @escapeNotVerified */ $block->getCurrentCategory()->getName() ?>

<?= /* @escapeNotVerified */ __('Shop By')  $block->getCurrentCategory()->getName() ?>

<?= /* @escapeNotVerified */ __('Shop By') ?> <?= $block->getCurrentCategory()->getName() ?>

Re: Get Current Category

<?= $block->escapeHtml(__('Shop By')) ?>
<?= $block->escapeHtml($block->getCurrentCategory()->getName()) ?>

This one

Was my answer helpful? You can accept it as a solution.
175+ Professional Extensions for M1 & M2
Need a developer?Just visit Contact Us Now