How to call cms static block in Magento 2

Written by Prashant Dhaduk

Mar 21, 2023

How to call cms static block in Magento 2

In this article, we will explain how to call a CMS static block in Magento 2.

  • Before continuing to the stages, let’s first clarify what is a cms block and why you need to use a cms block in Magento 2.

What is a CMS block in Magento 2?

  • Magento 2 static Cms blocks are used to display various types of content on one or more pages for advertising purposes. It could be a banner, images, or HTML code that appears in the front end.
  • Using CMS blocks you can display text, images, videos, and other static content anywhere on your Magento 2 site. you can use widgets in static blocks to add dynamic content in addition to static content.

Before calling the CMS block, you must first create a static block in Magento 2.

Step 1: Log in to Magento 2 and go to Content -> Blocks. Click “Add New Block”, fill in the below details, and save your configuration.

  1. Enable Block: Enable the block set Yes.
  2. Block Title: Name of the block to easily identify the purpose of creating the block.
  3. Block Identifier: Enter a unique block identifier.
  4. Store View: To display the static block, select the store view.

Step 2: Add your code to the block content. And then click the save button. When the static block is saved, it appears in the grid.

Now we can call the newly created static block from a layout XML, CMS page, and PHTML file.

  • Call CMS Static Block in Layout XML File

  • When we develop a custom module, you may need to insert a CMS block into your XML layout file. You can use the code below to call the CMS static block in the layout XML file. In the block id parameter, you must replace your block identifier.
<referenceContainer name="content">

    <block class="Magento\Cms\Block\Block" name="block_identifier">

        <arguments>

            <argument name="block_id" xsi:type="string">block_identifier</argument>

        </arguments>

    </block>

</referenceContainer>
  • Call CMS Static Block in CMS Page

  • If you’re making a static page and want to call some blocks on that page, paste the below code into the Cms page and replace it with your block identifier.
{{block class="Magento\Cms\Block\Block" block_id="block_identifier"}}
  • Call CMS Static Block in PHTML File

  • If you need to call a CMS block in a PHTML file, use the below code and replace the setBlockId argument with your block identifier.
<?= $this->getLayout()

->createBlock('Magento\Cms\Block\Block')

->setBlockId('block_identifier')

->toHtml();

?>

That’s it! I hope this technical blog will help you find what you were looking for.

If you have any further questions about Magento 2 cms static block or if you need any help from our Magento 2 expert, contact us now for a free consultation.

Bookmark it for your future reference. Do comment below if you have any other questions.

P.S. Do share this note with your team.

Prashant Dhaduk

Author

We can help you with

  • Dedicated Team
  • Setup Extended Team
  • Product Development
  • Custom App Development

Schedule a Developer Interview And Get 7 Days Risk-Free Trial

Fill out the form below, and one of our technical experts will reach out to you within 12 hours.

    Google
    |

    4.8

    Google
    |

    4.8

    Google
    |

    4.9

    Google
    |

    4.8

    Google
    |

    4.9

    Copyright © 2024 DOLPHIN WEB SOLUTION. All rights reserved.

    ×

    Hello!

    Click one of our contacts below to chat on WhatsApp

    × How can I help you?
    TO TOP