WordPress how to create dynamic sidebar with widget supportable

Hi, in this post I am going to show how to Develop wordpress dynamic sidebar with 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' => '<li id="%1$s" class="widget %2$s">',
'after_widget'  => '</li>',
'before_title'  => '<h2 class="widgettitle">',
'after_title'   => '</h2>',
) );

add_action( 'widgets_init', 'theme_slug_widgets_init' );
  1. 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.
  2. function theme_slug_widgets_init() is the function name which is called by the widget init
  3. register_sidebar() is the function foe register sidebar you van use multiple register_sidebar() function inside the function calling from the widget init.
  4. In side register_sidebar() function there should be an array of arguments.
  5. 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

<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>
<ul id="primary-sidebar" class="primary-sidebar widget-area" role="complementary">
<?php dynamic_sidebar( 'sidebar-1' ); ?>
</ul><!-- #primary-sidebar -->
<?php endif; ?>
  1. Here the ‘sidebar-1’ is the id of the sidebar declared in functions.php
  2. 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 <?php get_sidebar(); ?> 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 wordpress dynamic sidebar with widget for a custom wordpress theme.

Next we will learn about Develop WordPress custom post for slider with custom posr agguments


Thanks for Reading

Share The Post On -

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.