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
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
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() ?>
<?= $block->escapeHtml(__('Shop By')) ?>
<?= $block->escapeHtml($block->getCurrentCategory()->getName()) ?>This one