In some case you may want to register an additional sidebar for pages or a custom post type you’ve created. In this article, we’re going to learn how to register and add a custom sidebar to a custom post type.
First, we’re going to register our sidebar by adding the code below to our child-theme’s functions.php
// Custom Sidebar
function prefix_custom_sidebar() {
register_sidebar( array(
'name' => __( 'Custom Sidebar', 'page-builder-framework' ),
'id' => 'custom-sidebar',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h4 class="wpbf-widgettitle">',
'after_title' => '</h4>'
) );
}
add_action( 'widgets_init', 'prefix_custom_sidebar' );
Our new Sidebar should now appear under Appearance -> Widgets and can be filled with content.
In the next step, we’re going to display the sidebar on our custom post type by using a filter.
// Replace the default sidebar with our new custom sidebar on all docs posts
function prefix_do_custom_sidebar( $sidebar ) {
if( is_singular( 'docs' ) ) {
$sidebar ='custom-sidebar';
}
return $sidebar;
}
add_filter( 'wpbf_do_sidebar', 'prefix_do_custom_sidebar' );