此代码 (1) 的目的是以网格形式显示 Magento 存储中的子类别。类别图像的大小在代码中定义,而分隔和宽度在styles.css 中。
如果我正常使用代码,我会得到所有启用的子类别(IsActive 为真)。请参阅此“正在进行的工作”链接:http ://www.aldetal.biz/index.php/vabaro/fitness/nutricion.html
我更喜欢显示已启用并在其他地方标记为“值得展示”的子类别。这样我就可以将登录页面中以网格形式显示的子类别与菜单中显示的子类别分开。为了避免接触数据库,我将使用 Meta Keywords 字段添加字符串 showgrid 作为“关键字”标志。
代码
<?php
/**
* Original code by Jake Rutter
* @website: http://www.onerutter.com/web/magento-custom-category-images-listing-block-tutorial.html#idc-ctools
* Fixed by func0der
*
*/
?>
<div id="categories">
<div class="col_full">
<div class="listing" >
<?php
$_maincategorylisting = $this->getCurrentCategory();
$_categories = $this->getCurrentChildCategories();
if($_categories->count()):
foreach ($_categories as $_category):
/** Choose between the two possible IFs. The first one works (as before, incompletely. The second Should work completely but instead breaks the code */
/** if($_category->getIsActive()): */
if($_category->getIsActive() && preg_match('/^showgrid/ism', $_category->getData('meta_keywords'))):
$cur_category=Mage::getModel('catalog/category')->load($_category->getId());
$layer = Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($cur_category);
/** This is just for debugging. The first shows the sub-category name under the image (as it should in final version). The second one shows the text in Meta_Keywords. */
/**
/** $catName = $this->getCurrentCategory()->getName(); */
$catName = $this->getCurrentCategory()->getData('meta_keywords');
if($_imageUrl = $this->getCurrentCategory()->getImageUrl()):
?>
<div class="category-box">
<div class="category-image-box">
<a href="<?php echo $this->getCategoryUrl($_category)?>"><img src="<?php echo $_imageUrl?>" height="150"></a>
</div>
<div class="category-name">
<?php
/** Changed to check writing the category vs variable */
/** <a href="<?php echo $this->getCategoryUrl($_category)?>"> <?php echo $_category->getName()?></a> */
?>
<p>
<a href="<?php echo $this->getCategoryUrl($_category)?>"> <?php echo $catName ?></a>
</p>
</div>
</div>
<?php
else:
?>
<div class="category-box">
<div class="category-image-box">
<a href="<?php echo $this->getCategoryUrl($_category)?>"><img src="/skin/frontend/default/default/images/media/default01.jpg" height="150"); ?></a>
</div>
<div class="category-name">
<p>
<a href="<?php echo $this->getCategoryUrl($_category)?>"><?php echo $catName ?></a>
</p>
</div>
</div>
<?php
endif; /* END: if($_imageUrl=!$this->getCurrentCategory()->getImageUrl()) */
endif; /* END: $_category->getIsActive()) */
endforeach; /* END: ($_categories as $_category) */
/* This resets the category back to the original pages category
* If this is not done, subsequent calls on the same page will use the last category in the foreach loop.
*
* The next line is the one showing the problem. If I use the expanded IF I get the error here...
*/
$layer->setCurrentCategory($_maincategorylisting);
endif; /* END: if($_categories->count()) */
?>
</div>
<br clear=all>
</div>
</div>
出于某种原因,蒂姆建议的优秀 IF
(1) 最初由 Jake Rutter 写在这里:http : //www.onerutter.com/web/magento-custom-category-images-listing-block-tutorial.html 并由我调整
我有以下结构的代码来显示 Magento 前端的子类别。子类别在启用时显示(“IsActive”为真)。
if($_categories->count()):
foreach ($_categories as $_category):
if($_category->getIsActive()):
a whole bunch of stuff if true
else
other stuff if not true
我想在不弄乱数据库的情况下添加另一个条件,所以我使用了 Meta Keywords 字段(因为它被 Google 正式忽略了,无论如何拥有一个古怪的关键字并没有任何害处)。
如果启用了类别(IsActive)并且该字段中的第一个字符串是“showgrid”,则 IF 为真,并且发生了很多事情。
但要小心:元关键字字段可能为空,因此我需要对其进行测试并确定其内容。如果未启用类别(IsActive 为假)或元关键字为空或元关键字不以 showgrid 开头,则发生其他事情