Magento – Magento2 – How to create a new Page Layout

magento2page-layoutsPHP

I'm attempting to create a new page layout for Categories, here's what I have so far:

app/code/Karl/Subcategories/registration.php

<?php
use \Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Karl_Subcategories', __DIR__);

app/code/Karl/Subcategories/etc/module.xml

<config>
    <module name="Karl_Subcategories" setup_version="1.0.0"/>
</config>

app/code/Karl/Subcategories/view/frontend/layouts.xml

<page_layouts xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/PageLayout/etc/layouts.xsd">
    <layout id="subcategories">
        <label translate="true">Subcategories</label>
    </layout>
</page_layouts>

So far this is working, I now have a new option within the dropdown for "Custom Design > Page Layout".

But I'm having difficulty trying to define the actual page template which should be used and I'm unable to find much documentation on this.

app/code/Karl/Subcategories/view/frontend/page_layouts/subcategories.xml

<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_layout.xsd">
    <referenceBlock name="root">
        <action method="setTemplate">
            <argument name="template" xsi:type="string">Karl_Subcategories::subcategories.phtml</argument>
        </action>
    </referenceBlock>
</layout>

app/code/Karl/Subcategories/view/frontend/templates/subcategories.phtml

<?php
echo 'hi';

Any help is greatly appreciated.

Best Answer

You have keep subcategories.xml file inside app/code/Karl/Subcategories/view/frontend/page_layout folder. May be you have mistakes in folder name page_layout not page_layouts. plz check using above folder name and try again.