[英]How to get the Category name of product on product detail page in wordpress
[英]how to get category name of current product (on product detail page) in magento
我使用了以下代碼,但在這種情況下不起作用:
$_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');
if($is_product){
if(is_object(Mage::registry('current_category'))){
$category_name = Mage::registry('current_category')->getName();
}
else{ $category_name = ""; }
}
但這只適用於從類別到產品的情況。 如果您直接訪問產品頁面,則不會顯示任何內容
這是因為產品可以附加到多個類別。 在您的情況下,當您訪問從類別頁面引用的產品頁面時,您的會話具有類別信息。 但是,如果您直接訪問產品頁面,Magento無法知道您來自哪個類別,因此無法向您顯示特定類別,因為您的產品可能有多個類別。
但在您的情況下,如果您的產品只附加一個類別,您可以使用此代碼,它顯示產品的第一個類別名稱;
$categoryIds = $_product->getCategoryIds();
if(count($categoryIds) ){
$firstCategoryId = $categoryIds[0];
$_category = Mage::getModel('catalog/category')->load($firstCategoryId);
echo $_category->getName();
}
<?php
$_category_detail=Mage::registry('current_category');
echo $_category_detail->getName(); //gives current category name
echo $_category_detail->getId(); //gives current category id
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.