首页 文章

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

提问于
浏览
10

Quick Overview: 我试图将 static blocks 的特定集合中的结果返回到 Magento 中的phtml文件(然后从 cms page 调用) .

Note: 我've been searching all over google and some answers get me closer than others but nothing I'试过似乎100%工作?

Details:

我已经有一组特定的静态块,都以 testimonial- 的标识符开头 . 例如,每个静态块都是这样的: testimonial-1testimonial-2testimonial-3 等等 . 我在我的开发网站上总共有 5 (更多关于现场网站,但这并不重要) .

我有 CMS Page 代码拉入 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 . '
'; // just for testing $blockNum = 1; foreach($collection as $key => $value){ $_blockId = $this->getIdentifier(); $block_ID = $_blockId . $blockNum; echo "Key: " . $key . " - " . "Block ID: " . $block_ID . "
"; $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 ) . 由于我试图列出 all 推荐块,我怎样才能将 each 块id回显到页面?

对我来说很容易,我是php的初学者 .

1 回答

  • 8

    你不是在foreach循环中打印块 . 解决方案:将}括号移动到粘贴代码的末尾

    $blockNum = 1;
    foreach($collection as $key => $value){
        $_blockId = $this->getIdentifier();
        $block_ID = $_blockId . $blockNum;
        echo "Key: " . $key . " - " . "Block ID: " . $block_ID . "
    "; $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,这种方法是否正常 .

相关问题