我想根据产品页面中的类别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; ?>
您可以使用下面的代码来加载类别:
<?php $category = Mage::getModel('catalog/layer')->getCurrentCategory();?>
那么你可以得到这样的信息:
<?php echo $category->getName();?>
你可以在你的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; ?>
这是最终的代码,并且工作良好。
<?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 } ?>