Cyber Week Sale! Get $20 off GP Premium, $40 off our new Lifetime license, and 45% off license renewals/extensions! Learn more

[Resolved] How to register/display multiple sidebars

Home Forums Support How to register/display multiple sidebars

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #157570
    Peter Islin Nielsen

    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 πŸ™‚

    #157668
    Tom
    Lead Developer
    Lead Developer

    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/

    #157757
    Peter Islin Nielsen

    Yep, it probably would πŸ™‚ But on this specific project I would like to register and display it via functions.php if possible. Thanks!

    #157892
    Tom
    Lead Developer
    Lead Developer

    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

    #157897
    Peter Islin Nielsen

    Tom, you’re the real MVP πŸ™‚ Thanks!

    #157911
    Tom
    Lead Developer
    Lead Developer
    #157948
    Peter Islin Nielsen

    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.

    #158009
    Tom
    Lead Developer
    Lead Developer

    Any chance you can show me your page template for the post type?

    #158456
    Peter Islin Nielsen

    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.

    #158538
    Tom
    Lead Developer
    Lead Developer

    I agree – this would be a good knowledgebase post.

    On my list πŸ™‚

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.