Magento 2:如何覆盖Luma主题主页


9

我在WAMP Windows 10上使用Magento 2 CE版本2.1.0

我已经提到了

Magento 2:如何覆盖微型购物车默认模板html文件?

想要覆盖Magento 2默认Luma主题

我有以下文件夹结构

magento2
 |_ app
   |_ design
      |_ frontend
        |_ Custom
            |_Theme
              |_Magento_Theme
                |_templates
                  |_root.phtml     - Copy of Luma
                registration.php
                theme.xml

app \ design \ frontend \ Custom \ Theme \ Magento_Theme \ registration.php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::THEME,
    'frontend/Custom/Theme',
    __DIR__
);

app \ design \ frontend \ Custom \ Theme \ Magento_Theme \ theme.xml

<theme xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/theme.xsd">
    <title>My Theme</title>
    <parent>Magento/luma</parent>
</theme>

我也运行php bin/magento setup:static-content:deploy并清除缓存。它不会在中显示我新创建的主题Admin -> Content -> Design -> Configuration。编辑下拉列表。

我还缺少什么?


Answers:


6

创建任何新主题或模块时,需要在模块或主题文件夹的根目录下定义registration.php文件。

始终使用小写的主题名称,因为Magento使用此标准进行主题名称声明。

将主题名称保留在驼峰中并没有任何问题,但是使用标准方式将不胜感激。

您必须在Magento_Theme文件夹中的错误位置定义registration.php文件。

正确的主题结构图将在下面,

magento2
 |_ app
   |_ design
      |_ frontend
        |_ Custom
            |_theme
              |_Magento_Theme
                |_templates
                  |_root.phtml     - Copy of Luma
              |_registration.php
              |_theme.xml

您的registration.php路径是 app\design\frontend\Custom\theme\registration.php

registration.php文件:

<?php
   \Magento\Framework\Component\ComponentRegistrar::register(
     \Magento\Framework\Component\ComponentRegistrar::THEME,
      'frontend/Custom/theme',
      __DIR__
);

您的theme.xml文件路径将为

app\design\frontend\Custom\theme\theme.xml

theme.xml文件:

<theme xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/theme.xsd">
   <title>Custom Theme</title>
   <parent>Magento/luma</parent>
   <media>
      <preview_image>media/preview.jpg</preview_image>
   </media>
</theme>

完成所有设置后,请不要忘记运行主题的deploy命令,

php bin/magento setup:static-content:deploy

检查您的管理面板内部,Content -> Design -> Configuration设置您的自定义主题。

删除缓存并检入前端。


Bijal Usean和SH Patel。接受@Rakesh的答案,因为以前我正在与他讨论这个问题。
— Ankit Shah

从Admin保存时出现错误Cannot read contents from file "D:/wamp/www/magento2/" Warning!file_get_contents(D:/wamp/www/magento2/): failed to open stream: No such file or directory。因此无法保存和启用新主题
— Ankit Shah 2016年

请运行升级命令,然后运行deploy命令,从根目录删除var文件夹并检查
— Rakesh Jesadiya

不。仍然发行
— Ankit Shah


1

您将theme.xml和registration.php放在错误的位置,实际位置是

app/design/frontend/<vendor>/<theme>/registration.php

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::THEME,
    'frontend/<vendor>/<theme>',
    __DIR__
);

app/design/frontend/<vendor>/<theme>/theme.xml

<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<theme xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/theme.xsd">
    <title>Vendor Theme</title>
    <parent>Magento/luma</parent>
    <media>
        <preview_image>media/preview.jpg</preview_image>
    </media>
</theme>

注意:将Preview.jpg放在app/design/frontend/<vendor>/<theme>/media/preview.jpg

这两个文件足以在Admin中获取您的主题列表,然后应用您的主题。

例如app/code/Magento/Catalog/view/frontend/templates/product/view/addtocart.phtml,如果您想覆盖亮度模板,则可以在以下位置对该模板进行更改app/design/frontend/<vendor>/<theme>/Magento_Catalog/templates/product/view/addtocart.phtml


从管理员Cannot read contents from file "D:/wamp/www/magento2/" Warning!file_get_contents(D:/wamp/www/magento2/): failed to open stream: No such file or directory.保存时出现错误,因此无法保存和启用新主题
— Ankit Shah

似乎是文件权限问题,请检查是否按照magento标准设置了正确权限。
— Bilal Usean

正如所提到的那样I'm using Magento 2 CE Version 2.1.0 on WAMP Windows 10
— Ankit Shah


请检查您的magento根目录访问用户及其用户组,它应该是Web服务器用户及其组,并检查该目录的读/写权限。参照这两链接,您可以得到更多的想法devdocs.magento.com/guides/v2.0/install-gde/prereq/... magento.stackexchange.com/q/91870/36463
— 比拉尔Usean

1

您使用的路径不正确。

按照以下说明创建新的自定义主题。

使用以下代码在其中创建theme.xml文件 /app/design/frontend/Custom/Theme/theme.xml。

<theme xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/theme.xsd">
    <title>Custom Theme</title>
    <parent>Magento/luma</parent>
    <media>
        <preview_image>media/preview.jpg</preview_image>
    </media>
</theme>

app/design/frontend/Custom/Theme/registration.php使用以下代码在其中创建registration.php文件。

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::THEME,
    'frontend/Custom/theme',
    __DIR__
);

复制以下内容的preview.jpg

vendor/magento/theme-frontend-luma/media/preview.jpg 

添加

app/design/frontend/Custom/Theme/media/preview.jpg

现在,您可以在admin中看到您的自定义主题,从admin中选择自定义主题并保存。


从管理员Cannot read contents from file "D:/wamp/www/magento2/" Warning!file_get_contents(D:/wamp/www/magento2/): failed to open stream: No such file or directory.保存时出现错误,因此无法保存和启用新主题
— Ankit Shah

亮度主题保存工作正常吗?
— Suresh Chikani

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.