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

发布于 2025-01-04 15:06:54 字数 2214 浏览 1 评论 0原文

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

注意:我一直在谷歌上进行搜索,有些答案让我比其他答案更接近,但我尝试过的似乎都没有 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

Which is good.

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

别介意我,我是 php 的初学者。

Quick Overview: I am trying to return results from a specific set of static blocks to a phtml file (which is then called on from a cms page) in Magento.

Note: I've been searching all over google and some answers get me closer than others but nothing I've tried seems to work 100%?

Details:

I already have a set of specific static blocks that all start with an identifier of testimonial-. For example, each static block is like this: testimonial-1, testimonial-2, testimonial-3 and so on. I have a total of 5 on my dev site (more on live site but that is no consequence here).

I have a CMS Page with code pulling in the name.phtml file (location of my phtml file is here: app/design/frontend/[package]/[template]/template/page/):

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

Here is my code for the .phtml file:

<?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>

The loop foreach($collection as $key => $value) prints out this:

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

Which is good.

However, the only block that is echoed is the last block (testimonial-5). Since I'm trying to list out all the testimonial blocks, how can I echo out each block id to the page?

Go easy on me, I'm a beginner at php.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帝王念 2025-01-11 15:06:54

您没有在 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,那么这种方法可以吗?

You are not printing block inside foreach loop.
Solution: move } parenthesis to the end of pasted code

$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;
}

I think that on Magento Connect are some Testimonial Modules, that are doing job you want.
On the other hand, if you are looking for 'simple' solution or if you are trying to play with Magento, is this approach ok.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文