0

I've got a set of blocks made in the backend. Now, I want the following to be possible:

  • Iterate over a selected count of blocks, starting with a certain name (which would not be the hardest part) in a custom phtml file
  • Show each block like as a product on my page
  • When I click on a 'block product', a pop up will show up with content corresponding to that block.

Now, I already have the code for the pop up and stuff, but my question is:

Is it possible to iterate over a set of CMS blocks and show them on my page (and preferably in the same way a product, so more like a link to the block rather than the whole block content) ?

1 Answer 1

0

I did it as follows:

<ul class="block-list-grid">
<?php
// maximum of a hundred blocks
for ($i = 0; $i < 100; $i++) {
    if (!empty($block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('block_' . $i)->toHtml())):?>
       // have corresponding image for each block and iterating over them
        <li class="block-list-image">
            <div class="block-list-grid-item" data-option="<?= $i ?>">
                <?= $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('img_block_' . $i)->toHtml(); ?>
            </div>
        </li>
    <?php
    endif;
}
?>
    </ul>
<?php
for ($i = 0; $i < 100; $i++) {
if (!empty($block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('block_' . $i)->toHtml())):?>
<div class="story-items">
    <div class="pop-up-mtm" data-option="<?= $i ?>">
        <div class="pop-up-text mtm-text-pop-up">
            <span class="close-popup-mtm"></span>
            <?= $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('block_' . $i)->toHtml(); ?>
        </div>
    </div>
</div>
<?php
endif;
}
?>

So as you can see, I made blocks starting with 'img_block_', which contain images. The next loop that iterates over 'block_' is the content in the pop ups. When clicked on for instance the image of 'img_block_1', the popup with content of 'block_1' will show up.

A relative small piece of simple code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.