Create sidebar.php for original theme
This is one of the articles about project “Create Original Theme in WordPress”.
GOAL
To create sidebar.php
Environment
WordPress 5.5.1
XAMPP 7.4.10
sidebar.php
<?php /** * The template used for the sidebar *@package WordPress *@subpackage Techblog *@since Techblog 1.0 */ ?> <?php if ( is_active_sidebar('sidebar') ) : ?> <aside id="sidebar"> <?php dynamic_sidebar('sidebar'); ?> </aside> <?php endif; ?>
functions.php
‘widgets_init’
‘widgets_init’ is the hook to initialize widgets such as sidebar. You should hook the register function register_sidebar() with add_action(). Please check the article “What is Hook in WordPress?” for detail about hook in WordPress.
In my case, techblog_widgets_init() is the function to register sidebar.
/*sidebar*/ function techblog_widgets_init() { register_sidebar( array( 'name' => esc_attr__( 'SideBar', 'techblog' ), 'id' => 'sidebar', 'before_widget' => '<div id="%1$s" class="widget %2$s">', 'after_widget' => '</div>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>', ) ); } add_action( 'widgets_init', 'techblog_widgets_init' );
Add widget
Add widgets into the created sidebar into the created sidebar with name “SideBar”.