我有两个帖子类别,有两种不同的布局,但现在两者都显示在同一个视图中。phtml。我需要创建一个检查帖子属于哪个类别,并相应地显示样式。
通过使用以下方法,我可以加载ID为2的单个类别。
<?php $test = Mage::getModel('wordpress/term')->load(2);?>
有没有办法加载所有的帖子类别。?
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; ?>
通过这种方法,您可以根据类别拆分帖子,并用不同的布局显示在同一个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; ?>