我有两个具有两种不同布局的帖子类别,但现在两者都显示在同一个view.phtml中。我需要检查帖子所属的类别并相应地显示样式。
通过使用以下方法,我可以加载 ID 为 2 的单个类别。
<?php $test = Mage::getModel('wordpress/term')->load(2);?>
有没有办法加载所有的帖子类别。?
夏姆快到了。这是代码的稍微干净的版本:
<?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; ?>
通过这种方法,您可以根据类别拆分帖子并以不同的布局显示在同一个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; ?>