我想根据产品页面中的类别ID有条件地加载内容,但无法使其工作。我知道我可以通过调用自定义属性来实现这一点,但这不是我想到的,因为内容专门针对特定类别,所以我不想在给定类别的每个产品中重复输入它。
基本上我一直在寻找这样的东西:
<?php if ($_catid = $this->getCategoryid('3')):?>
display content for category id 3 (content is entered directly in the view.phtml file)
<?php else: ?>
content for other cateogegory
<?php endif; ?>
非常感谢您的指导!
更新正确的代码(感谢ahadley):
<?php $category = Mage::getModel('catalog/layer')->getCurrentCategory();?>
<?php if($category->getId()==3): ?>
<h3>Show content for Category ID3</h3>
<?php else: ?>
<h3>Show content for other categories</h3>
<p>consectetuer adipiscing elit. </p>
<?php endif; ?>
答案 0 :(得分:2)
您可以使用以下内容加载类别:
<?php $category = Mage::getModel('catalog/layer')->getCurrentCategory();?>
然后你可以得到如下信息:
<?php echo $category->getName();?>
答案 1 :(得分:1)
您可以在product/view.phtml
模板中执行以下操作:
<?php if (Mage::registry('current_category') == 3): ?>
// display content for category with the ID 3
<?php else: ?>
// content for other categories
<?php endif; ?>
答案 2 :(得分:0)
这是最终的代码,并且工作正常。
<?php if (Mage::registry('current_category') && Mage::registry('current_category')->getId() == 290) { ?>
<?php
$categoryIds = $_product->getCategoryIds();
$m = Mage::getModel('catalog/category')
->load($categoryIds[0])
->getParentCategory();
echo $m->getName();
?>
<?php } else ?>
<?php if (Mage::registry('current_category') && Mage::registry('current_category')->getId() == 202) { ?>
<?php
$categoryIds = $_product->getCategoryIds();
$m = Mage::getModel('catalog/category')
->load($categoryIds[2])
->getParentCategory();
echo $m->getName();
?>
<?php } else { ?>
<?php
$categoryIds = $_product->getCategoryIds();
$m = Mage::getModel('catalog/category')
->load($categoryIds[1])
->getParentCategory();
echo $m->getName();
?>
<?php } ?>