use*_*255 6 php magento magento-1.5
我使用了以下代码,但在这种情况下不起作用:
$_category_detail=Mage::registry('current_category');
echo $_category_detail->getName();
Run Code Online (Sandbox Code Playgroud)
得到致命错误:在/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 = ""; }
}
Run Code Online (Sandbox Code Playgroud)
但这只适用于从类别到产品的情况.如果您直接访问产品页面,则不会显示任何内容
muh*_*edv 23
这是因为产品可以附加到多个类别.在您的情况下,当您访问从类别页面引用的产品页面时,您的会话具有类别信息.但是,如果您直接访问产品页面,Magento无法知道您来自哪个类别,因此无法向您显示特定类别,因为您的产品可能有多个类别.
但在您的情况下,如果您的产品只附加一个类别,您可以使用此代码,它显示产品的第一个类别名称;
$categoryIds = $_product->getCategoryIds();
if(count($categoryIds) ){
$firstCategoryId = $categoryIds[0];
$_category = Mage::getModel('catalog/category')->load($firstCategoryId);
echo $_category->getName();
}
Run Code Online (Sandbox Code Playgroud)