How to Set WooCommerce Different Sidebar For Each Category

WooCommerce Different Sidebar For Each CategoryAre you looking for a quick and easy way to register a sidebar automatically for each category? In today’s brief tutorial, we will show you how you can do this by using custom code snippets to modify the sidebar template file. This is the most recommended way for making modifications because you will not bloat your site.

If you run an affiliate marketing store, you may want to spotlight very specific advertisers based on the categories on the site. This means that we need to dynamically create the sidebars without hard-coding each sidebar on the site. This will save a lot of time, and you will not need to use any plugin.

Since we will be editing the core files, you need to create or install a child theme before you proceed. This will ensure that your changes are not lost when you update your theme or WooCommerce.

It is also important to have a complete backup of your site. This will ensure that you can revert to the previous version if an error occurs.

WooCommerce Different Sidebar For Each Category

The main aim of this post is to help you automatically add a widget area for adding advertisements or any other content. In addition, we will also show you how you can exclude any categories that you wish to target.

Let us get right into it.

Steps to Create Sidebars Automatically For Each Category

Here are the simple steps you need to follow:

  1. Log into your WordPress site and access the Dashboard as the admin user.
  2. From the Dashboard menu, click on Appearance Menu > Theme Editor Menu. When the Theme Editor page is opened, look for the theme functions file to add the function to create sidebars automatically for each category.
  3. Add the following code to the php file. Remember you can include and exclude any categories that you wish to target. Within the foreach statement, you can modify and match the layout to your overall WordPress site’s sidebar formatting.
function njengah_category_sidebars() {
    $args = array(
        'type'                     => 'post',
        'orderby'                  => 'name',
        'order'                    => 'ASC',
        'hide_empty'               => 1,
        'hierarchical'             => 1,
        'exclude'                  => '',
        'include'                  => '',
        'number'                   => '',
        'taxonomy'                 => 'category'
        ); 
        $categories = get_categories($args);
    foreach ($categories as $category) {
        if (0 == $category->parent)
            register_sidebar( array(
                'name' => $category->cat_name,
                'id' => $category->category_nicename . '-sidebar',
                'description' => 'This is the ' . $category->cat_name . ' widgetized area',
                'before_widget' => '<aside id="%1$s" class="widget %2$s">',
                'after_widget' => '</aside>',
                'before_title' => '<h3 class="widget-title">',
                'after_title' => '</h3>',
            ));
        }
}
add_action( 'widgets_init', 'njengah_category_sidebars' );

  1. To see if a sidebar exists and has a widget added to it, add the following code to the same file:
function is_sidebar_active($cat_name) {
    global $wp_registered_sidebars;
    $cat_id = get_cat_ID($cat_name);
    $widgetlist = wp_get_sidebars_widgets();
    if ($widgetlist[$cat_id])
        return true;
    return false;
}

  1. Open the sidebar template file(), and add the following code to dynamically display the area if the sidebar is registered and has a widget in it:
$queried_object = get_queried_object();
if ($queried_object) {
    $post_id = $queried_object->ID;
}
if(is_category() || in_category($cat_name, $post_id)) {
    $sidebar_id = sanitize_title($cat_name);
    if( is_sidebar_active($sidebar_id)) {
        dynamic_sidebar($sidebar_id);
    }
}
  1. This is the outcome:widgets

Wrapping Up

By now, you should be able to create a different sidebar for each category automatically. It is worth mentioning that you do not need a plugin because they end up bloating your site.

If you need any custom solution, feel free to contact us. However, we hope that this post provided a solution to your problem.

Similar Articles


Customer Reviews

NJENGAH

Customer Reviews

Jeffrey Todd 04/07/2020

Exceptional Plugin & Service

I'm in the process of building my new website, and I had a question about changing some text within the WooCommerce plugin. I was spinning my wheels trying to figure it out. Joe reached back and worked with me - going above and beyond! - until I fully understood how to effectively change what I wanted. How can you ask for more than that? I highly recommend WooCommerce (and Joe!), the plugin and service are exceptional!
-Jeffrey Todd (MyLifeChart.com)