在类别列表页面上显示自定义文本字段
Show custom text field on category list page
我添加了一个自定义模块,用于向类别页面添加额外的描述。它显示在管理员中,但我无法让它显示在类别前端页面上。我可能不了解childtheme继承结构。
我已经阅读并尝试了这里的每一篇文章,但其中 none 提供了我需要的准确信息。
- Luma Childtheme 已激活:app>design>frontend>MyCompany>Luma_child
- 自定义模块:app>code>MyCompany>CategoryAttribute
我试过添加 app/code/MyCompany/CategoryAttribute/view/frontend/templates/myCustomFile.phtml 和 CategoryAttribute/view/frontend/layout/catalog_category_view.xml
以及 app/design/frontend/MyCompany/Luma_child
中的类似内容
希望在首页上看到我的自定义类别文本,但事实并非如此。没有错误显示。
经过大量研究,我能够自己解决问题。因此,为了我自己将来参考或帮助任何面临同样问题的人,它是:
使用 this guide.
构建模块以将自定义字段添加到类别
然后,要在分类列表页面的前端显示值,请按照下列步骤操作:
1) 在您的模块 'view' 文件夹 (app/code/YourName/YourModule/view) 中创建一个名为 'frontend'
的文件夹
2) 在这个文件夹中我们还需要两个文件夹:'layout' 和 'templates'
3) 在 'layout' 中创建一个名为 'catalog_category_view.xml' 的文件并添加以下代码:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Magento\Framework\View\Element\Template" name="YourModule" template="YourName_YourModule::products.phtml" />
</referenceContainer>
</body>
</page>
4) 在 'templates' 中创建一个名为 'products.phtml' 的文件并添加代码:
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
echo $category->getCustomCategoryField(); ?>
确保通过 SSH 应用所有更改。如果没有显示,请尝试清除浏览器缓存。
我添加了一个自定义模块,用于向类别页面添加额外的描述。它显示在管理员中,但我无法让它显示在类别前端页面上。我可能不了解childtheme继承结构。
我已经阅读并尝试了这里的每一篇文章,但其中 none 提供了我需要的准确信息。
- Luma Childtheme 已激活:app>design>frontend>MyCompany>Luma_child
- 自定义模块:app>code>MyCompany>CategoryAttribute
我试过添加 app/code/MyCompany/CategoryAttribute/view/frontend/templates/myCustomFile.phtml 和 CategoryAttribute/view/frontend/layout/catalog_category_view.xml 以及 app/design/frontend/MyCompany/Luma_child
中的类似内容希望在首页上看到我的自定义类别文本,但事实并非如此。没有错误显示。
经过大量研究,我能够自己解决问题。因此,为了我自己将来参考或帮助任何面临同样问题的人,它是:
使用 this guide.
构建模块以将自定义字段添加到类别然后,要在分类列表页面的前端显示值,请按照下列步骤操作:
1) 在您的模块 'view' 文件夹 (app/code/YourName/YourModule/view) 中创建一个名为 'frontend'
的文件夹2) 在这个文件夹中我们还需要两个文件夹:'layout' 和 'templates'
3) 在 'layout' 中创建一个名为 'catalog_category_view.xml' 的文件并添加以下代码:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Magento\Framework\View\Element\Template" name="YourModule" template="YourName_YourModule::products.phtml" />
</referenceContainer>
</body>
</page>
4) 在 'templates' 中创建一个名为 'products.phtml' 的文件并添加代码:
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
echo $category->getCustomCategoryField(); ?>
确保通过 SSH 应用所有更改。如果没有显示,请尝试清除浏览器缓存。