If it was in M1, we have to create a layout handle named `<category_id>` under local.xml and add blocks or customize as accordingly. But now in magento 2 neither here is local.xml nor a layout handle. We need to have a different xml file for each layout handle we used to have in M1.
For eg. for a homepage layout handle in Magento 1.x `<cms_index_index/>` = we have to create a xml file `cms_index_index.xml` in Magento 2
Now let’s get to the creating different template file for different categories in Magento 2. Obviously the identifier should be category id. We can achieve that by creating a new xml file named `catalog_category_view_id_{{id}}.xml` under our theme > Magento_Catalog > layout.
For eg. for a category id. 23, the xml file we need to create is `catalog_category_view_id_23.xml` Â and change the list.phtml file under this.
9 Comments
-
-
-
-
-
-
-
-
-
Jasper
December 21, 2016
I created a catalog_category_view_id_43.xml but it keeps using the catalog_category_view.xml file (which is in the same folder).
When I remove this catalog_category_view.xml file the catalog_category_view_id_43 file is working fine.
Do you know how to solve this?
Fetch Designs
March 31, 2017
Thank you… this absolutely worked for me! I couldn’t get it working using the “Layout Update XML” field in the admin on the category because we already had a global setting in our theme’s catalog_category_view.xml extend file to set all categories to DESC by default. Here’s my related issue: http://magento.stackexchange.com/q/167048/2415
Another issue we had was that our previous developer had just copied the catalog_category_view.xml into the theme instead of creating a fresh file and just using referenceContainer and referenceBlock to tweak the few changes they wanted to make so I had to clean that up. Not sure that’s related to the issue Jasper had, but thought I would throw it out there just in case.
webastral
June 22, 2017
Hello, i created a custom layout of a category with catalog_Category_vies_id.xml but , i need to assign that template and functionality to many categories , and i dont want to make xml for every category, please give me a soluction how i can assign that template to other categories. i will be waiting for your response.
Thanks.
Suman K.C
June 22, 2017
Hi Webastral, if you have many categories you would have a same layout then change the default category layout file and for those remaining create individual layout file.
Thanks.
webastral
June 22, 2017
No, i dont want to change my default category layout, because many categories are working on the default layout. i just want a same layout for 4 or 5 categories, i dont know how to do that, i make a layout for one category with catalog_Category_vies_id.xml , the problem is , it working for only one category, how i assign this xml to the other 4 or 5 categories, i tried with update layout xml in the admin. but did not work. I hope you understand, if you have any soluction , please reply me.
Thanks
Suman K.C
June 22, 2017
Hi again, the solution covers for a category i.e. one layout file covers for a single category.
For your solution i don’t see any other solution other than creating multiple file for different categories(4/5 categories on your case). Thanks.
webastral
June 22, 2017
okay thanks for your time.
Dustin
June 22, 2018
I have the same problem as webastral. I have hundreds of categories (300+) and half are on one page layout and half are on the other… so what do it do?
Suman K.C
July 5, 2018
Hi Dustin,
This best scenario for this solution is when few out of every other category have different layout. For your case it isn’t worth to create template file for half of the categories. Please let me know if you have figured out the solution for until now.
Leave a Reply