1

I've two Post Categories with two different layouts, But now both are displaying in the same view.phtml. I need to create a check in which category the post belongs and display the style accordingly.

By using below method, I can load a single category with ID 2.

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

Is there any way to load all the post categories.?

Shyam Prakash
  • 52
  • 1
  • 7

2 Answers2

0

By this method, you can split posts according to category and display in the same view.phtml with different layouts, for adding different layouts paste your code inside the if($getCategory == cat_id) section as I mentioned below.

    <?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 Prakash
  • 52
  • 1
  • 7
0

Shyam is almost there. Here is a slightly cleaner version of the code:

<?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; ?>
Ben Tideswell
  • 1,697
  • 2
  • 11
  • 14