Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento - How do you return results of unlimited CMS Static blocks (with certain "Identifier") to a CMS Page

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.

like image 465
Geoff Avatar asked Feb 10 '12 18:02

Geoff


People also ask

How do I get CMS block data by identifier in Magento 2?

You can get CMS Static Blocks collection by an identifier in Magento 2 by calling BlockRepositoryInterface interface. Magento\Cms\Api\BlockRepositoryInterface used for getting CMS Static Blocks in Magento 2.

How do I get CMS block collection in Magento 2?

Use the below code snippet if you want to show CMS Static Block from template phtml file in Magento 2: echo $this->getLayout() ->createBlock('Magento\Cms\Block\Block') ->setBlockId('your_block_identifier') ->toHtml();


1 Answers

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.

like image 196
Jakub Šimon Avatar answered Oct 23 '22 19:10

Jakub Šimon