开发者

Conditional loading content based on category id magento for product page

开发者 https://www.devze.com 2023-04-06 09:55 出处:网络
I want to conditionally load content based on category id in product page but can\'t get it to work. I know I can do this by calling a custom attribute but this is not what I have in mind because the

I want to conditionally load content based on category id in product page but can't get it to work. I know I can do this by calling a custom attribute but this is not what I have in mind because the content is specifical开发者_StackOverflowly for a particular category so I don't want to enter it repeatedly in each product for the given category.

Basically I was looking for something like this:

<?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; ?>

Your guidance is greatly appreciate!

Update for the correct code (thanks 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; ?>


You could use something like the following to load the category:

<?php $category = Mage::getModel('catalog/layer')->getCurrentCategory();?>

Then you could get information such as:

 <?php echo $category->getName();?>


You can do something like this in your product/view.phtml template:

<?php if (Mage::registry('current_category') == 3): ?>
// display content for category with the ID 3
<?php else: ?>
// content for other categories
<?php endif; ?>


This is final code, and working fine.

 <?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 } ?>
0

精彩评论

暂无评论...
验证码 换一张
取 消