2016-07-28 1 views

答えて

5

試してみてください。

$categoryId = 3; 
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$category = $_objectManager->create('Magento\Catalog\Model\Category') 
->load($categoryId); 
echo $category->getName(); 
9

あなたはObjectManagerを使用しないでください。

あなたはブロックでこれを入れて、PHTMLに機能getCategoryName()を呼び出すことができます。

namespace Company\Module\Block; 

class CustomBlock extends \Magento\Framework\View\Element\Template 
{ 
    protected $_categoryFactory;  

    public function __construct(
    \Magento\Catalog\Model\CategoryFactory $categoryFactory, 
    \Magento\Framework\View\Element\Template\Context $context, 
    ) { 
     $this->_categoryFactory = $categoryFactory; 
     parent::__construct($context); 
    } 

    public function getCategoryName() 
    { 
     $categoryId = '43'; 
     $category = $this->_categoryFactory->create()->load($categoryId); 
     $categoryName = $category->getName(); 
     return $categoryName; 
    } 
} 
関連する問題