在类别列表页面上显示自定义文本字段 [英] Show custom text field on category list page

查看:102
本文介绍了在类别列表页面上显示自定义文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've added a custom module for adding an extra description to category pages. It's showing in Admin but I can't get it to show on category frontend pages. I'm probably not understanding the childtheme inheritance structure.

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

I've read and tried every article here but none of them provide the exact information I need.

  • 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

I've tried adding app/code/MyCompany/CategoryAttribute/view/frontend/templates/myCustomFile.phtml and CategoryAttribute/view/frontend/layout/catalog_category_view.xml and also something similar in app/design/frontend/MyCompany/Luma_child

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

Expected to see my custom category text on frontpage but it's not. No errors showing.

推荐答案

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

After a lot of research I was able to solve the problem myself. So for future reference for myself or to help anyone facing the same issue here it is:

构建一个模块,以使用本指南.

Build a module to add a custom field to categories using this guide.

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

Then, to show the value in frontend on the category list page, follow these steps:

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

1) In your module 'view' folder (app/code/YourName/YourModule/view) create a folder named 'frontend'

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

2) In this folder we need two more folders: 'layout' and 'templates'

3)在布局"中,创建一个名为"catalog_category_view.xml"的文件,并添加以下代码:

3) In 'layout' create a file called 'catalog_category_view.xml' and add the following code:

<?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>

<?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"的文件并添加代码:

4) In 'templates' create a file called 'products.phtml' and add code:

<?php 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
echo $category->getCustomCategoryField(); ?>

确保通过SSH应用所有更改.如果未显示,请尝试清除浏览器缓存.

Make sure to apply all changes through SSH. If it's not showing try clearing your browser cache.

这篇关于在类别列表页面上显示自定义文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆