Php 如何在Magento 2中显示静态块?

Php 如何在Magento 2中显示静态块?,php,magento2,Php,Magento2,我正在创建一个magento 2主题。 我想在cms主页上显示自定义块 我想知道如何在phtml和xml布局、cms页面内容中显示静态块 我该怎么办?请尝试使用以下代码 在Phtml文件中: <?php echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('block_identifier')->toHtml();?> 在Xml文件中: <referen

我正在创建一个magento 2主题。 我想在cms主页上显示自定义块


我想知道如何在phtml和xml布局、cms页面内容中显示静态块


我该怎么办?

请尝试使用以下代码

在Phtml文件中:

<?php echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('block_identifier')->toHtml();?>
在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\u index\u index.xml
Magento\u cms
文件夹中(这应该放在您自己的包/主题文件夹中,而不是
Magento/blank
app/code


只需将上述xml代码放入该文件的
标记中,根据缓存设置,您可能需要清除它们以查看调整。

使用以下代码在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();?>

这些都不能通过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>

标题链接
社交
148
43

我想知道如何在phtml和xml布局中显示静态块,cms页面内容。使用Magento 2.0.2。出于测试目的,我将上面提供的xml插入到产品页面“自定义布局更新”,并将php块插入到
app/design/frontend/Magento///Magento_Catalog/templates/product/view/details.phtm
。我还是看不见这个街区。我做错了什么吗?我需要像这样设置其他参数
    <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>