Magento 2 Create Custom Category Widget.

In Magento 2, There are many default widgets available in native.
You can check the list of the widget from Magento Opensource by Admin panel.

Go To Content -> Elements -> Widgets

Name of widgets which are an available default,
CMS Page link, CMS Static Block, Catalog Category Link, Catalog New Products List, Catalog Product Link, Catalog Products List, Orders and Returns, Recently Compared Products, Recently Viewed Products

We are going to create a new custom widget for display Category grid. In which you have to select the category from widget admin panel and based on the selected category you can get a grid of category with Category Image and name at a specific location.

For creating a custom widget you need to create a custom module.
For Declare module we need to create registration.php and module.xml file,
Let’s start with custom module Packagename is Rbj and Module name is CategoryWidget.

Create file, app/code/Rbj/CategoryWidget/registration.php

module.xml file, app/code/Rbj/CategoryWidget/etc/module.xml 

For create a Custom widget, We need to create a widget.xml file under the, etc folder.
Using widget.xml You can define your custom widget to the admin panel.

File Path, app/code/Rbj/CategoryWidget/etc/widget.xml

For Define our custom logic, We need to create block class in the above file,
Our custom block file is Rbj\CategoryWidget\Block\Category\Widget\CategoryList and they implement BlockInterface from \Magento\Widget\Block\BlockInterface.

In the above XML file, We have defined two fields for no. of a category to display in a grid and the second field is for conditions in which you need to select categories for display in the widget.

Now we need to define our block file of a widget,

Create CategoryList.php file,

Now we need to create a template file which we have assigned to widget.xml file for display in the frontend.

In the above code from the widget.xml file,

We have called two phtml files in a widget,
Create a phtml file for the Main content area,


Create the phtml file for sidebar area,


Run command to install our module,

Now you can go to widget section of Magento backend by refer below steps,
Go To Content -> Elements -> Widgets.
Click on Add Widget.


Custom widget
Custom widget in Magento 2

Now you need to set category from conditions tab,

category widget
select category from the category tree