このコード (1) の意図は、Magento ストアのサブカテゴリをグリッド形式で表示することです。カテゴリ画像のサイズはコード自体で定義され、分離と幅は styles.css で定義されます。
コードを通常どおり使用すると、有効になっているすべてのサブカテゴリが取得されます (IsActive が true)。この「進行中の作業」リンクを参照してください: http://www.aldetal.biz/index.php/vabaro/fitness/nutricion.html
有効になっていて、他の場所で「表示に値する」というフラグが付けられているサブカテゴリを表示したいと思います。このようにして、ランディング ページのグリッド形式で表示されるサブカテゴリの数と、メニューに表示されるサブカテゴリの数を分けています。データベースに触れないようにするために、「キーワード」フラグとして文字列 showgrid を追加する Meta Keywords フィールドを使用します。
コード
<?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" が true)。
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 は true であり、さまざまな処理が行われます。
ただし、注意してください。Meta Keywords フィールドは空である可能性があるため、内容を確認するだけでなく、テストする必要があります。カテゴリが有効になっていない (IsActive が false) またはメタ キーワードが空である、またはメタ キーワードが showgrid で始まらない場合、他のことが起こります