Are 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:
- Log into your WordPress site and access the Dashboard as the admin user.
- 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.
- 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.
$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’ ); [/php]
- To see if a sidebar exists and has a widget added to it, add the following code to the same file:
global $wp_registered_sidebars;
$cat_id = get_cat_ID($cat_name);
$widgetlist = wp_get_sidebars_widgets();
if ($widgetlist[$cat_id])
return true;
return false;
} [/php]
- 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:
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);
}
}
[/php]
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
- How to Get Registered Sidebars WordPress with Foreach loop
- How to Add Currency to WooCommerce [Custom Currency]
- How to Add New Column On WooCommerce Orders Page
- How to Duplicate a Product in WooCommerce
- 15+ Best WordPress Maintenance Services » WordPress Website Care
- How to Move WooCommerce Products To New Site
- WordPress Database: Complete Guide from Novice to Expert
- How to Create Multiple Widgets in WordPress Using For & Foreach Loops
- How to Display Products by Category Shortcode WooCommerce
- How to Set WooCommerce Related Products Same Category
- How to Remove Product Category Title WooCommerce
- How to Create Checkbox Field in WooCommerce Checkout Page
- How to Remove Uncategorized Category In WordPress WooCommerce
- How to Set Up WooCommerce Buy One Get One
- How to Select All Except Last Child In CSS » CSS Not Last Child Example
- Wix vs WordPress Complete Comparison » [Features, Pros & Cons]
- How to Get Current Product Category Name in WooCommerce
- How to Add WooCommerce Registration Email Verification
- How to Speed Up WooCommerce Website