微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – Magento – 如何将无限CMS静态块(具有特定的“标识符”)的结果返回给CMS页面

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

注意:我一直在谷歌搜索,一些答案让我比别人更近,但我没有尝试似乎工作100%?

细节:

我已经有一组特定的静态块,全部以一个证明的标识符开头.例如,每个静态块如下所示:testimonial-1,testimonial-2,testimonial-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

哪个是好的

然而,唯一被回覆的块是最后一个块(见证-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,这种方法是否正常.

原文地址:https://www.jb51.cc/php/132116.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐