0

enter image description here

I want to show Category name below the product name

here is the code:

<div class="block widget block-products-list <?= /* @noEscape */ $mode ?>">
    <?php if ($block->getTitle()) : ?>
        <div class="block-title">
            <strong><?= $block->escapeHtml(__($block->getTitle())) ?></strong>
        </div>
    <?php endif ?>
    <div class="block-content">
        <?= /* @noEscape */ '<!-- ' . $image . '-->' ?>
        <div class="products-<?= /* @noEscape */ $mode ?> <?= /* @noEscape */ $mode ?>">
            <ol class="product-items <?= /* @noEscape */ $type ?>">
                <?php $iterator = 1; ?>
                <?php foreach ($items as $_item) : ?>
                    <?= /* @noEscape */ ($iterator++ == 1) ? '<li class="product-item">' : '</li><li class="product-item">' ?>
                    <div class="product-item-info">
                        <a href="<?= $block->escapeUrl($block->getProductUrl($_item)) ?>" class="product-item-photo">
                            <?= $block->getImage($_item, $image)->toHtml() ?>
                        </a>
                        <div class="product-item-details">
                            <strong class="product-item-name">
                                <a title="<?= $block->escapeHtml($_item->getName()) ?>"
                                   href="<?= $block->escapeUrl($block->getProductUrl($_item)) ?>"
                                   class="product-item-link">
                                    <?= $block->escapeHtml($_item->getName()) ?>
                                </a>
                            </strong>
                            <?php if ($templateType) : ?>
                                <?= $block->getReviewsSummaryHtml($_item, $templateType) ?>
                            <?php endif; ?>

                            <?= $block->getProductPriceHtml($_item, $type) ?>

                            <?= $block->getProductDetailsHtml($_item) ?>

                            <?php if ($showWishlist || $showCompare || $showCart) : ?>
                                <div class="product-item-inner">
                                    <div class="product-item-actions">
                                        <?php if ($showCart) : ?>
                                            <div class="actions-primary">
                                                <?php if ($_item->isSaleable()) : ?>
                                                    <?php $postParams = $block->getAddToCartPostParams($_item); ?>
                                                    <form data-role="tocart-form" data-product-sku="<?= $block->escapeHtml($_item->getSku()) ?>" action="<?= $block->escapeUrl($postParams['action']) ?>" method="post">
                                                        <input type="hidden" name="product" value="<?= $block->escapeHtmlAttr($postParams['data']['product']) ?>">
                                                        <input type="hidden" name="<?= /* @noEscape */ Action::PARAM_NAME_URL_ENCODED ?>" value="<?= /* @noEscape */ $postParams['data'][Action::PARAM_NAME_URL_ENCODED] ?>">
                                                        <?= $block->getBlockHtml('formkey') ?>
                                                        <button type="submit"
                                                                title="<?= $block->escapeHtml(__('Add to Cart')) ?>"
                                                                class="action tocart primary">
                                                            <span><?= $block->escapeHtml(__('Add to Cart')) ?></span>
                                                        </button>
                                                    </form>
                                                <?php else : ?>
                                                    <?php if ($_item->getIsSalable()) : ?>
                                                        <div class="stock available"><span><?= $block->escapeHtml(__('In stock')) ?></span></div>
                                                    <?php else : ?>
                                                        <div class="stock unavailable"><span><?= $block->escapeHtml(__('Out of stock')) ?></span></div>
                                                    <?php endif; ?>
                                                <?php endif; ?>
                                            </div>
1

You can do this way ..

There are 2 ways to get category Data:

=> Using Object Manager:

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance(); 
$appState = $objectManager->get('\Magento\Framework\App\State');
$appState->setAreaCode('frontend');
$registry = $objectManager->get('\Magento\Framework\Registry');
$currentCategory = $registry->registry('current_category');
echo $currentCategory->getName() . '<br />';
echo $currentCategory->getUrl() ;

=> Using Block :

1) Create Block:

app/code/Jaydip/HelloWorld/Block/HelloWorld.php

<?php
namespace Jaydip\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
        protected $_registry;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Framework\Registry $registry,
        array $data = []
    )
    {        
        $this->_registry = $registry;
        parent::__construct($context, $data);
    }

    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }

    public function getCurrentCategory()
    {        
        return $this->_registry->registry('current_category');
    }

    public function getCurrentProduct()
    {        
        return $this->_registry->registry('current_product');
    }    

}
?>

2) Get current category in any template (.phtml) file

// print current category data
$currentCategory = $block->getCurrentCategory();
echo $currentCategory->getName() . '<br />';

Other steps are you can call phtml file in Theme and layout

/app/design/frontend/Vendor/Theme/Magento_Catalog/layout/catalog_product_view.xml

<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>

        <block class="Magento\Catalog\Block\Product\View"
                   name="product.info.category.name"
                   template="Magento_Catalog::product/view/categoryName.phtml" after="page.main.title"/>

    </body>
</page>

/app/design/frontend/Vendor/Theme/Magento_Catalog/templates/product/view/categoryName.phtml

<?php 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');
$categories = $product->getCategoryIds(); /*will return category ids array*/
foreach($categories as $category){
    $cat = $objectManager->create('Magento\Catalog\Model\Category')->load($category);
        echo $cat->getName();
    }

?>
|improve this answer|||||

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.