sidebars allow you to display widgets inside your theme.
And yes, despite the name, you can use “sidebars” to display widgets anywhere you want.
By default, themes come with at least one sidebar.
In this post, I’m going share with you a quick way to add a custom sidebar to your WordPress theme.
If you’re using a custom theme, skip this step. However if you’re using a theme maintained by someone else that may be updated in future, I recommend you create a child theme to leave the original intact.
Install the Child Theme Configurator plugin and copy the required template files such as single.php and page.php into the child theme. The step 4 explains this part.
Add this code into functions.php in order to register your custom sidebar:
function my_custom_sidebar() {
register_sidebar(
array (
'name' => __( 'Custom', 'your-theme-domain' ),
'id' => 'custom-side-bar',
'description' => __( 'Custom Sidebar', 'your-theme-domain' ),
'before_widget' => '<div class="widget-content">',
'after_widget' => "</div>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
)
);
}
add_action( 'widgets_init', 'my_custom_sidebar' );
I want to render the custom sidebar in single posts only, so I’ll edit the “Single post” file.
<?php if ( is_active_sidebar( 'custom-side-bar' ) ) : ?>
<?php dynamic_sidebar( 'custom-side-bar' ); ?>
<?php endif; ?>
I placed the code above in the location where I want the sidebar to be visible, then I save the changes.