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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了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-"}}@H_419_10@ 
 

这是我的.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>@H_419_10@ 
 

循环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@H_419_10@ 
 

哪个是好的

然而,唯一被回覆的块是最后一个块(见证-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;
}@H_419_10@ 
 

我认为在Magento Connect上有一些推荐模块,那就是你想做的工作.另一方面,如果您正在寻找“简单”的解决方案,或者如果您正在尝试使用Magento,这种方法是否正常.

脚本宝典总结

以上是脚本宝典为你收集整理的php – Magento – 如何将无限CMS静态块(具有特定的“标识符”)的结果返回给CMS页面全部内容,希望文章能够帮你解决php – Magento – 如何将无限CMS静态块(具有特定的“标识符”)的结果返回给CMS页面所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。