Hi, in this post I am going to show how to Develop WordPress dynamic sidebar with a widget for a custom WordPress theme, for that
First: Go to function.php file and write the below code to declare a sidebar
//To Register a sidebar function theme_slug_widgets_init() { register_sidebar( array( 'name' => __( 'Main Sidebar', 'theme-slug' ), 'id' => 'sidebar-1', 'description' => __( 'Widgets in this area will be shown on all posts and pages.', 'theme-slug' ), 'before_widget' => '
', 'after_title' => '
', ) ); } add_action( 'widgets_init', 'theme_slug_widgets_init' );- Here the add_action( ‘widgets_init’, ‘theme_slug_widgets_init’ ); the parameter ‘widgets_init’is for init the widget and ‘theme_slug_widgets_init’ is the name of the function to be call.
- function theme_slug_widgets_init() is the function name which is called by the widget init
- register_sidebar() is the function foe register sidebar you van use multiple register_sidebar() function inside the function calling from the widget init.
- Inside register_sidebar() function there should be an array of arguments.
- Here the id ‘sidebar-1’ will be used to get the sidebar widgets
Second: you have declared the sidebar and widget can be added from admin panel now open the sidebar.php if not created already please create a sidebar.php file inside your theme folder
Third: write the below code to get the widgets attached with the sidebar
- Here the ‘sidebar-1’ is the id of the sidebar declared in functions.php
- dynamic_sidebar( ); is the function to get the sidebar and inside that have to pass the id of the registered sidebar
Fourth: Save all the files add widget from admin panel to the registered sidebar and function in any pages to get the sidebar.php file and then run the file. And you have done.
In this post, we have learned how to Develop a WordPress dynamic sidebar with the widget for a custom WordPress theme.
Next, we will learn about Develop WordPress custom post for slider with custom post arguments
Thanks for Reading