[英]How to show the static blocks in Magento 2?
我正在創建一個magento 2主題。 我想在cms主頁上顯示自定義塊。
我想知道在phtml和xml布局中顯示靜態塊,cms頁面內容
我能怎么做?
請嘗試使用以下代碼。
在Phtml文件中:
<?php echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('block_identifier')->toHtml();?>
在CMS內容中:
{{block class="Magento\\Cms\\Block\\Block" block_id="block_identifier"}}
在Xml文件中:
<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>
以上是正確的,另外,主頁的xml文件是cms_index_index.xml
文件夾中的Magento_Cms
(這應該在你自己的包/主題文件夾中,而不是在Magento/blank
或app/code
)。
只需將上面的xml代碼放在該文件的<body>
標記內,具體取決於清除它們以查看調整所需的緩存設置。
這些都不適用於通過default.xml放置靜態塊請提供適當的示例。 在2.0.7當我在我的主題中應用以下主題后,在名為socialimg / socialimg的管理中創建一個靜態塊作為塊標識符,則不顯示任何內容。 甚至在記憶中。 甚至沒有搜索exception.log或system.log
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="header.panel">
<block class="Magento\Framework\View\Element\Html\Links" name="header.links">
<arguments>
<argument name="css_class" xsi:type="string">header links</argument>
</arguments>
</block>
</referenceContainer>
<referenceBlock name="socialimg1">
<block class="Magento\Cms\Block\Block" name="socialimg">
<arguments>
<argument name="block_id" xsi:type="string">socialimg</argument>
</arguments>
</block>
</referenceBlock>
<referenceBlock name="logo">
<arguments>
<argument name="logo_img_width" xsi:type="number">148</argument>
<argument name="logo_img_height" xsi:type="number">43</argument>
</arguments>
</referenceBlock>
<referenceContainer name="footer">
<block class="Magento\Store\Block\Switcher" name="store_switcher" as="store_switcher" after="footer_links" template="switch/stores.phtml"/>
</referenceContainer>
<referenceBlock name="report.bugs" remove="true"/>
<move element="copyright" destination="before.body.end"/>
</body>
</page>
使用以下代碼在CMS頁面中顯示靜態塊 -
{{block class="Magento\\Cms\\Block\\Block" block_id="block_identifier"}}
使用以下代碼在phtml文件中顯示靜態塊 -
<?php echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('block_identifier')->toHtml();?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.