php – Magento – 如何将无限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,这种方法是否正常. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |