問題描述
我使用了以下代碼,但不適用于這種情況:
$_category_detail=Mage::registry('current_category');echo $_category_detail->getName();
得到致命錯誤:在/app/design/frontend/base/default/template/catalog/product/view.phtml中的非對象上調用成員函數getName()>
我們制作了一些過濾器并在 head.phtml 中使用以下提及的代碼:
$is_product = Mage::registry('product');如果($is_product){if(is_object(Mage::registry('current_category'))){$category_name = Mage::registry('current_category')->getName();}else{ $category_name = "";}}
但這僅在您從類別轉到產品時才有效.如果您直接訪問產品頁面,則不會顯示任何內容
這是因為產品可以附加到多個類別.在您的情況下,當您訪問從類別頁面引用的產品頁面時,您的會話具有類別信息.但是如果您直接訪問產品頁面,Magento 無法知道您來自哪個類別,因此無法顯示特定類別,因為您的產品可以有多個類別.
但在您的情況下,如果您的產品只附加一個類別,您可以使用此代碼,它顯示產品的第一個類別名稱;
$categoryIds = $_product->getCategoryIds();如果(計數($categoryIds)){$firstCategoryId = $categoryIds[0];$_category = Mage::getModel('catalog/category')->load($firstCategoryId);echo $_category->getName();}
I used following codes but didn't work for this case:
$_category_detail=Mage::registry('current_category');
echo $_category_detail->getName();
got Fatal error: Call to a member function getName() on a non-object in /app/design/frontend/base/default/template/catalog/product/view.phtml
we make some filters and use below mention code in head.phtml:
$is_product = Mage::registry('product');
if($is_product){
if(is_object(Mage::registry('current_category'))){
$category_name = Mage::registry('current_category')->getName();
}
else{ $category_name = ""; }
}
But this only works if you go from a category to a product. If you visit the product page directly nothing is being displayed
It's because products can be attached to multiple categories. In your situation, when you visit a product page referred from a category page, your session has category information. But if you visit directly product page, Magento can not know which category you came from, so it can not show you a specific category, because your product can have multiple categories.
But in your situation, if your products are attached only one category, you can use this code, it shows first category name of product;
$categoryIds = $_product->getCategoryIds();
if(count($categoryIds) ){
$firstCategoryId = $categoryIds[0];
$_category = Mage::getModel('catalog/category')->load($firstCategoryId);
echo $_category->getName();
}
這篇關于如何在 magento 中獲取當前產品的類別名稱(在產品詳細信息頁面上)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!