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

2022-11-06php开发问题
2

本文介绍了Magento - 您如何将无限 CMS 静态块(具有特定“标识符")的结果返回到 CMS 页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

注意:我一直在谷歌上搜索,有些答案让我比其他答案更接近,但我尝试过的任何东西似乎都没有 100% 有效?

详情:

我已经有一组特定的静态块,它们都以 testimonial- 的标识符开头.例如,每个静态块是这样的:testimonial-1testimonial-2testimonial-3等等.我的开发站点上总共有 5 个(更多在实时站点上,但在此处无关紧要).

我有一个 CMS 页面,其中包含 name.phtml 文件中的代码(我的 phtml 文件的位置在这里:app/design/frontend/[包]/[模板]/模板/页面/):

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

这是我的 .phtml 文件代码:

getCollection()->addFieldToFilter('identifier', array('like'=>'testimonial'.'%'))->addFieldToFilter('is_active', 1);//获取计数$blockCount = $collection->count();echo '块计数:'.$blockCount .'<br/>';//仅用于测试$blockNum = 1;foreach($collection as $key => $value){$_blockId = $this->getIdentifier();$block_ID = $_blockId .$blockNum;回声键:".$key .——".块ID:".$block_ID ."<br/>";$blockNum++;}$_block = $this->getLayout()->createBlock('cms/block')->setBlockId($block_ID);如果($_block):?><div class="block block-testimonial"><div class="block-title"><strong><?php echo $this->getTitle();?></strong>

<div class="block-content"><?php echo $_block->toHtml();?>

循环 foreach($collection as $key => $value) 打印出这个:

Key: 27 - 区块 ID: testimonial-1键:28 - 区块 ID: testimonial-2键:29 - 区块 ID:testimonial-3键:30 - 区块 ID: testimonial-4键:31 - 区块 ID:testimonial-5

哪个好.

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

放轻松,我是 php 的初学者.

解决方案

您没有在 foreach 循环内打印块.解决方法:将}括号移到粘贴代码的末尾

$blockNum = 1;foreach($collection as $key => $value){$_blockId = $this->getIdentifier();$block_ID = $_blockId .$blockNum;回声键:".$key .——".块ID:".$block_ID ."<br/>";$blockNum++;$_block = $this->getLayout()->createBlock('cms/block')->setBlockId($block_ID);如果 ($_block) : ?><div class="block block-testimonial"><div class="block-title"><strong><?php echo $this->getTitle();?></strong>

<div class="block-content"><?php echo $_block->toHtml();?>

<?php万一;}

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

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.

解决方案

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.

这篇关于Magento - 您如何将无限 CMS 静态块(具有特定“标识符")的结果返回到 CMS 页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用

PHP实现DeepL翻译API调用

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法

PHP通过phpspreadsheet导入Excel日期数据处理方法

PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件

mediatemple - 无法使用 codeigniter 发送电子邮件

mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误

Laravel Gmail 配置错误

Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题

将 PHPMailer 用于 SMTP 的问题

Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题

Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17

热门文章

1nohup:忽略输入并将输出附加到“nohup.out" 2在控制台中出错:无法加载资源:net::ERR_CONNECTION_RESET 3如何将 LDAP 时间戳转换为 Unix 时间戳 4不推荐使用常量 FILTER_SANITIZE_STRING 5APACHE 崩溃:父进程:子进程以状态 3221225477 退出 -- 正在重新启动 6PHP通过phpspreadsheet导入Excel日期数据处理方法 7Analytics API 返回:错误请求 - invalid_grant 8“tlsv1 警报内部错误"握手时

热门精品源码

最新VIP资源

1多功能实用站长工具箱html功能模板 2多风格简历在线生成程序网页模板 3论文相似度查询系统源码 4响应式旅游景点宣传推广页面模板 5在线起名宣传推广网站源码 6酷黑微信小程序网站开发宣传页模板 7房产销售交易中介网站模板 8小学作业自动生成程序