在类别列表页面上显示自定义文本字段

问题描述 投票:0回答:1

我已经添加了一个自定义模块,用于为类别页面添加额外的描述。它显示在Admin中,但无法显示在类别前端页面上。我可能不了解childtheme继承结构。

我已经在这里阅读并尝试了每篇文章,但是都没有提供我需要的确切信息。

  • Luma Childtheme处于活动状态:应用程序>设计>前端> MyCompany> Luma_child
  • 自定义模块:应用程序>代码> 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中的类似内容

希望在首页上看到我的自定义类别文本,但事实并非如此。没有错误显示。

frontend categories magento2
1个回答
0
投票

经过大量研究,我自己能够解决问题。因此,对于我自己或将来对遇到相同问题的任何人提供参考,是:

[构建一个模块以使用this guide.将自定义字段添加到类别中

然后,在类别列表页面上的前端显示值,请按照下列步骤操作:

1)在模块的“视图”文件夹(应用程序/代码/ YourName / YourModule /视图)中,创建一个名为“ frontend”的文件夹

2)在此文件夹中,我们还需要两个文件夹:“布局”和“模板”

3)在“布局”中,创建一个名为“ 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)在“模板”中创建一个名为“ 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应用所有更改。如果未显示,请尝试清除浏览器缓存。

© www.soinside.com 2019 - 2024. All rights reserved.