How To Registering A Sidebar And Use In Your Custom Theme In WordPress

Here we learn about how to register a sidebar and use it in your custom theme In WordPress.

Custom code copied and paste into your child theme’s functions.php file.

1. register_sidebar()

It has more parameters see below the code example.

<?php
function myblog_widgets_init() {
  register_sidebar( array(
    'name'          => esc_html__( 'Footer Column 3', 'text-domain' ),
    'id'            => 'myblog-footer-col-3',
    'description'   => esc_html__( 'Add widgets here.', 'text-domain' ),
    'before_widget' => '<section id="%1$s" class="widget %2$s">',
    'after_widget'  => '</section>',
    'before_title'  => '<h2 class="widget-title">',
    'after_title'   => '</h2>',
  ) );
  
}
add_action( 'widgets_init', 'myblog_widgets_init' );
?>

The following list of the code parameters.

  • name – widget sidebar name.
  • id – your theme using dynamic_sidebar function and string must be lowercase.
  • description – The sidebar description. It displays in the admin Widgets panel.
  • class – you can add CSS class name to assign.
  • before_widget – It is placed before every widget HTML.
  • after_widget – IT is placed after every widget HTML.
  • before_title –It is placed before the title of each widget HTML.
  • after_title – It is placed after every title HTML.

2. dynamic_sidebar()

How to display the sidebar using the dynamic_sidebar function.

Add below code in the footer file:

<?php
<div class="footer-bottom">
  <div class="container">
    <div class="footer3-sec">
      <?php 
      if ( is_active_sidebar( 'myblog-footer-col-3' ) ) :
        dynamic_sidebar( 'myblog-footer-col-3' );
      endif;
      ?>
    </div>
  </div>
</div>
?>

OUTPUT:

1) Set widget in the admin

2) footer display

Submit a Comment

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

Subscribe

Select Categories