Archived topic
How to register/display multiple sidebars
9 replies · Started by Peter Islin Nielsen on December 3, 2015
You've previously helped me with setting up a custom sidebar on WooCommerce pages. Now I need to display another sidebar on a custom post type called "Nyheder". I've copied and changed all theme files as you instructed me to do the last time – but how do I get my functions.php file to display it? This is how my functions.php looks now – I need to register and display the sidebar "Nyheder Sidebar":
function arphabet_widgets_init() {
register_sidebar( array(
'name' => 'WooCommerce Sidebar',
'id' => 'woocommerce_sidebar',
'before_widget' => '<aside id="%1$s" class="widget inner-padding %2$s">',
'after_widget' => '</aside>',
'before_title' => apply_filters( 'generate_start_widget_title', '<h4 class="widget-title">'),
'after_title' => apply_filters( 'generate_end_widget_title', '</h4>' ),
) );
register_sidebar( array(
'name' => 'Nyheder Sidebar',
'id' => 'nyheder_sidebar',
'before_widget' => '<aside id="%1$s" class="widget inner-padding %2$s">',
'after_widget' => '</aside>',
'before_title' => apply_filters( 'generate_start_widget_title', '<h4 class="widget-title">'),
'after_title' => apply_filters( 'generate_end_widget_title', '</h4>' ),
) );
}
add_action('widgets_init','arphabet_widgets_init');
add_action('generate_woocommerce_sidebars','generate_contsruct_sidebars');
function generate_contsruct_sidebars()
{
get_sidebar('woocommerce');
}
I think I need help with the last lines. Thanks :-)
Hi Peter,
Not sure if I've asked you or not, but wouldn't a plugin like this work?: https://wordpress.org/plugins/custom-sidebars/
Yep, it probably would :-) But on this specific project I would like to register and display it via functions.php if possible. Thanks!
You could try something like:
add_action( 'get_footer','generate_post_type_sidebars', 0 );
function generate_post_type_sidebars()
{
if ( 'your-post-type' ==! get_post_type() )
return;
do_action( 'generate_post_type_sidebars' );
}
add_action('generate_post_type_sidebars','generate_construct_post_type_sidebars');
function generate_construct_post_type_sidebars()
{
get_sidebar('posttype');
}
That would assume you had a file named: sidebar-posttype.php
Tom, you're the real MVP :-) Thanks!
No problem! :)
Hi Tom,
I'm afraid I'm doing something wrong here. When I use this:
add_action( 'get_footer','generate_post_type_sidebars', 0 );
function generate_post_type_sidebars()
{
if ( 'nyheder' ==! get_post_type() )
return;
do_action( 'generate_post_type_sidebars' );
}
add_action('generate_post_type_sidebars','generate_construct_post_type_sidebars');
function generate_construct_post_type_sidebars()
{
get_sidebar('nyheder');
}
...the sidebar displays on all posts – not only the custom post type (called "nyheder"). And why does it contain the "get_footer" part? Thanks.
Any chance you can show me your page template for the post type?
Hi Tom,
I decided to use a plugin like you said, so it’s all good now :-) But when you find the time, it would be cool with a guide of some sort in your knowledgebase, where it’s explained how to setup multiple sidebars and widgets in GeneratePress. Just a thought.
I agree - this would be a good knowledgebase post.
On my list :)
