Magento-如何将无限CMS静态块的结果(带有特定的“标识符”)返回到CMS页面


Magento - How do you return results of unlimited CMS Static blocks (with certain "Identifier") to a CMS Page

快速概述:我正试图将一组特定的静态块的结果返回到Magento中的phtml文件(然后从cms页面调用)。

注意:我一直在谷歌上搜索,有些答案让我比其他答案更接近,但我尝试过的似乎都没有100%有效?

详细信息:

我已经有了一组特定的静态块,它们都以标识符testimonial-开头。例如,每个静态块是这样的:testimonial-1testimonial-2testimonial-3等等。我的开发站点上总共有5(在实时站点上有更多,但这在这里没有影响)。

我有一个CMS页面name.phtml文件中有代码提取(我的phtml文件的位置在这里:app/design/frontend/[package]/[template]/template/Page/):

{{block type="core/template" template="page/name.phtml" title="Others Say:" identifier="testimonial-"}}

以下是我的.phtml文件代码:

<?php
    // add the collection with filters
$collection = Mage::getModel('cms/block')->getCollection()
    ->addFieldToFilter('identifier', array('like'=>'testimonial'.'%'))
    ->addFieldToFilter('is_active', 1);
// get the count
$blockCount = $collection->count();
    echo 'Block Count: ' . $blockCount . '<br />'; // just for testing
$blockNum = 1;
foreach($collection as $key => $value){
    $_blockId = $this->getIdentifier();
    $block_ID = $_blockId . $blockNum;
    echo "Key: " . $key . " - " . "Block ID: " . $block_ID . "<br />";
    $blockNum++;
}
$_block = $this->getLayout()->createBlock('cms/block')->setBlockId($block_ID);
if ($_block) :
?>
<div class="block block-testimonial">
<div class="block-title">
    <strong><?php echo $this->getTitle(); ?></strong>
</div>
<div class="block-content">
<?php echo $_block->toHtml(); ?>
</div>

循环foreach($collection as $key => $value)打印出以下内容:

Key: 27 - Block ID: testimonial-1
Key: 28 - Block ID: testimonial-2
Key: 29 - Block ID: testimonial-3
Key: 30 - Block ID: testimonial-4
Key: 31 - Block ID: testimonial-5

这很好。

然而,唯一被回显的块是最后一个块(testimonial-5)。由于我试图列出所有的推荐区块,我如何将每个区块id回显到页面?

放开我,我是php的初学者。

您没有在foreach循环内打印块。解决方案:将}括号移到粘贴代码的末尾

$blockNum = 1;
foreach($collection as $key => $value){
    $_blockId = $this->getIdentifier();
    $block_ID = $_blockId . $blockNum;
    echo "Key: " . $key . " - " . "Block ID: " . $block_ID . "<br />";
    $blockNum++;    
    $_block = $this->getLayout()->createBlock('cms/block')->setBlockId($block_ID);
    if ($_block) : ?>
        <div class="block block-testimonial">
            <div class="block-title">
                <strong><?php echo $this->getTitle(); ?></strong>
            </div>
        <div class="block-content">
        <?php echo $_block->toHtml(); ?>
        </div>
    <?php 
    endif;
}

我认为在Magento Connect上有一些证明模块,它们正在做你想要的工作。另一方面,如果你正在寻找"简单"的解决方案,或者你正在尝试使用Magento,这种方法可以吗?