如何从product_viewed.phtml获取product/view.phtml中的类别名称? [英] How to get the category name in product/view.phtml from product_viewed.phtml?

查看:111
本文介绍了如何从product_viewed.phtml获取product/view.phtml中的类别名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从product_viewed.phtml列表中获得类别名称?

How can I get the category name from product_viewed.phtml list?

登陆产品页面frontend\base\default\template\catalog/product/view.phtml

如果我单击frontend\base\default\template\reports/product_viewed.phtml

这是我view.phtml

<?php
$_helper = $this->helper('catalog/output');
$_category_detail = Mage::registry('current_category');
var_dump($_category_detail->getName()); //gives current  category name
?>

这是我得到的错误,

致命错误:在非对象中调用成员函数getName() C:\ xxx \ frontend \ base \ default \ template \ catalog \ product \ view.phtml在 第86行

Fatal error: Call to a member function getName() on a non-object in C:\xxx\frontend\base\default\template\catalog\product\view.phtml on line 86

但是,如果我单击frontend\base\default\template\catalog/product/list.phtml

您知道如何从这两种不同情况中获得类别名称吗?

Any idea how can I get the category name from these two different situations?

推荐答案

我认为您可以通过加载产品模型从产品名称或ID中获取类别ID.

I think you can get the category id from product name or id by loading model of product.

例如以下代码.

  $catCollection = $_product->getCategoryCollection();
    foreach($catCollection as $cat){
      print_r($cat->getData());
      //echo $cat->getName();
      //echo $cat->getUrl();
    }

另一种方式,

$_product= Mage::getModel('catalog/product');
$catIds = $_product->getCategoryIds();

之后,您可以通过访问$ catIds数组(例如$ catIds [0],$ catIds [1]等)来获取类别.

After that you can get the categories by accessing $catIds array such as $catIds[0], $catIds[1] etc.

这篇关于如何从product_viewed.phtml获取product/view.phtml中的类别名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆