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

[Resolved] How do I add a custom sidebar?

Home Forums Support How do I add a custom sidebar?

Viewing 15 posts - 1 through 15 (of 20 total)
  • Author
    Posts
  • #138321
    Peter Islin Nielsen

    Hi Tom!

    First of all: It’s an absolute pleasure to work with this awesome theme! Everything is so smooth, tidy and well crafted. Keep up the good work 🙂

    There is one thing, though, that I can’t wrap my head around: How do I add a custom sidebar to my WooCommerce pages? I don’t want to use one of the standard sidebars that comes with GP, so I’m afraid this will not work in my case. I’ve come as far as to register the custom sidebar in my functions.php, and it also shows up nicely in the WP backend. But what do I do next? I’ve looked a bit at the do_action(‘generate_sidebars’), but I think I need some guidance…

    Thanks in advance 🙂

    #138375
    Tom
    Lead Developer
    Lead Developer

    Hi Peter,

    Glad you’re enjoying GP 🙂

    Are you just wanting to display different widgets on your WooCommerce pages? If so, this plugin should help: https://wordpress.org/plugins/display-widgets/

    #138379
    Peter Islin Nielsen

    Hi Tom,

    Yeah, it’s by far the best theme I’ve worked with. It beats them all… Better throw you a good review when I get the time 🙂

    I’ve already tried out Display Widgets, Widget Context and other plugins of that sort – and it works, but it doesn’t quite get me all the way. It works great with the display/hide widgets on my other pages and post, but on my WooCommerce pages I need a fresh sidebar. Is it possible? It will make it so much easier for my client to maintain that way.

    #138382
    Tom
    Lead Developer
    Lead Developer

    Thanks!

    What exactly do you mean by “fresh sidebar”? Using the plugins you listed is probably the easiest way.

    This plugin is cool, but I’m not sure if it’s integrated with WooCommerce: https://wordpress.org/plugins/custom-sidebars/

    #138387
    Peter Islin Nielsen

    In the WP backend I have a lot of standard GP widgets like “Right Sidebar”, “Left Sidebar” and so on. I would like an extra sidebar callede “WooCommerce Sidebar” where I can put all my WooCommerce related stuff to show on all WooCommerce pages. Normally I would register a new widget in my functions.php and display it on a template with <?php dynamic_sidebar( 'woocommerce_sidebar' ); ?>, but I can’t quite get it to work in GP.

    #138413
    Tom
    Lead Developer
    Lead Developer

    Ah, so you would have to duplicate the sidebar.php or sidebar-left.php files found in the parent theme.

    Call it sidebar-woocommerce.php or something. This file would be the same as the others, but would call your custom widget area.

    Then duplicate the woocommerce.php file found in the parent theme, and add it to your child theme.

    In there, find do_action('generate_sidebars') and replace it with: do_action('generate_woocommerce_sidebars')

    Then you would add a function like this:

    add_action('generate_woocommerce_sidebars','generate_construct_sidebars');
    function generate_construct_sidebars()
    {
    	get_sidebar('woocommerce'); 
    	
    }

    That should get you on your way 🙂

    #138429
    Peter Islin Nielsen

    Yes, that’s exactly what I want 🙂

    But it doesn’t seem to work – I’ve added this to my functions.php:

    function arphabet_widgets_init() {
    
    	register_sidebar( array(
    		'name'          => 'WooCommerce Sidebar',
    		'id'            => 'woocommerce_sidebar',
    		'before_widget' => '<div>',
    		'after_widget'  => '</div>',
    		'before_title'  => '<h4>',
    		'after_title'   => '</h4>',
    	) );
    
    }
    add_action( 'widgets_init', 'arphabet_widgets_init' );
    
    add_action('generate_woocommerce_sidebars','generate_contsruct_sidebars');
    function generate_contsruct_sidebars()
    {
    	get_sidebar('woocommerce'); 
    	
    }

    Then I copied the sidebar.php file from GP and copied it to my child theme folder. I renamed it to sidebar-woocommerce.php. After that I copied the woocommerce.php from GP and placed it in my child theme folder as well. I found the do_action('generate_sidebars') and changed it to do_action('generate_woocommerce_sidebars') in woocommerce.php.

    In the WP backed the widget shows up – and I can interact with it and drag things onto it. But it doesn’t show on the front page.

    #138514
    Tom
    Lead Developer
    Lead Developer

    Inside your sidebar-woocommerce.php file did you edit the call to the correct registered sidebar?

    #138668
    Peter Islin Nielsen

    Auch, I totally forgot to do that. Thanks, Tom. Premium support once again 🙂

    #138670
    Tom
    Lead Developer
    Lead Developer
    #251049
    Matt

    Tom,

    I am trying to do the same thing as Peter was here a year ago, but it looks like the woocommerce.php file has changed since 1.3.22 and there is no do_action(‘generate_sidebars’). How to proceed?

    Alternatively, I’ve tried the DisplayWidgets plugin you mentioned and several other custom sidebar plugins without any luck on the WooCommerce pages. I just need a right-sidebar that displays shop-related widgets instead of the blog-related widgets.

    Thanks for any help.

    #251055
    Tom
    Lead Developer
    Lead Developer

    Ah yes, GP no longer has a woocommerce.php file and uses hooks instead.

    Let’s try something like this…

    add_action( 'wp','tu_woocommerce_sidebar' );
    function tu_woocommerce_sidebar()
    {
        if ( function_exists( 'is_woocommerce' ) && is_woocommerce() ) {
            remove_action( 'generate_sidebars','generate_construct_sidebars' );
            add_action( 'generate_sidebars','tu_construct_woocommerce_sidebar' );
        }
    }
    
    function tu_construct_woocommerce_sidebar() {
        get_sidebar( 'woocommerce' );
    }

    You’ll still need to register the sidebar etc.. However this should replace the need for changing the woocommerce.php file.

    Let me know 🙂

    #252935
    Thierry

    Hello,

    I have a similar problem, i know that it’s possible with widget but for other reason i search an other way.

    In a child theme i create single-newpage.php, content-newpage.php and sidebar-newpage.php

    In single-newpage.php i replace

    <?php 
    do_action('generate_sidebars');
    get_footer();

    by

    <?php 
    do_action('generate_newpage_sidebars');
    get_footer();

    It’s correct ?

    other thing, what is the code for functions.php ?

    thanck you

    #253047
    Tom
    Lead Developer
    Lead Developer

    That shouldn’t necessary as this code does that for you: https://generatepress.com/forums/topic/how-do-i-add-a-custom-sidebar/#post-251055

    Then you just need to create your widget area with the ID woocommerce: https://codex.wordpress.org/Function_Reference/register_sidebar

    #254649
    Thierry

    but why Woocommerce ? i don’t use woo for this website

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