显示Magento Fishpig中的所有帖子类别

时间:2016-03-29 07:48:36

标签: magento fishpig

我有两个不同布局的帖子类别,但现在两个都显示在相同的 view.phtml 中。我需要创建一个检查帖子属于哪个类别并相应地显示样式。

通过使用以下方法,我可以加载ID为2的单个类别。

<?php $test = Mage::getModel('wordpress/term')->load(2);?>

有没有办法加载所有帖子类别。?

2 个答案:

答案 0 :(得分:0)

通过这种方法,您可以根据类别拆分帖子并在不同布局的同一view.phtml中显示,添加不同的布局会将您的代码粘贴到if($getCategory == cat_id)部分中,如下所述。

    <?php $categories = $post->getTermCollection('category') ?>
    <?php if (count($categories) > 0): ?>
    <?php foreach($categories as $category): ?>
    <?php
     $getCategory = $this->escapeHtml($category->getId());
            echo "Get cat: ".$getCategory;
    if($getCategory == 2)
     {
       //your code here
     }
    if($getCategory == 3)
         {
           //your code here
         }
<?php endforeach; ?>
<?php endif; ?>

答案 1 :(得分:0)

Shyam几乎就在那里。这是一个稍微清洁的代码版本:

<?php $categories = $post->getTermCollection('category') ?>
<?php if (count($categories) > 0): ?>
    <?php foreach($categories as $category): ?>
        <?php if ((int)$category->getId() === 1): ?>
            // Category ID #1
        <?php elseif ((int)$category->getId() === 2): ?>
            // Category ID #2       
        <?php else: ?>
            // All other categories
        <?php endif; ?>
    <?php endforeach; ?>
<?php endif; ?>