在 Magento Fishpig 中显示所有 Post 类别

Display all the Post Categories in Magento Fishpig

我有两个具有两种不同布局的 Post 类别,但现在它们都显示在相同的 view.phtml 中。我需要检查 post 属于哪个类别并相应地显示样式。

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

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

有什么方法可以加载所有 post 个类别吗?

通过这种方法,您可以根据类别拆分帖子并以不同的布局显示在相同的 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; ?>

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