WordPress: Create Sidebars Automatically For Each Category

Function to Register Sidebars for Each WordPress Category

I’ve been simplifying this site to improve speed times and to attempt to monetize the site better without irritating my readers. There are multiple ways that I’ve monetized the site… here they are from most to least lucrative:

  • Direct sponsorships from partner companies. We work on collective strategies that incorporate everything from webinars to social media shares to promote their events, products, and/or services.
  • Affiliate marketing from an array of affiliate platforms. I scour and identify the companies, ensure they’re reputable, and share specific articles I write or ads that they provide.
  • Resource marketing from a partner who releases marketing-related events, case studies, and white papers.
  • Banner advertising from Google where relevant ads are automatically dispersed through my template and content.

WordPress Sidebars

With affiliate marketing providing some decent revenue, I decided that I wanted to spotlight very specific advertisers based on the category of the site, so I wanted to dynamically create the sidebars without having to hard-code each sidebar on the site. This way, if I add a category – the sidebar automatically appears in my Widget area and I can add an advertisement.

To do this, I needed some specific code in the functions.php file of my child theme. Thankfully, I found that someone had already written nearly everything that I needed: Create Widgetized Sidebars for Each Category in WordPress. I just wanted some additional controls over which categories I may wish to display the sidebars in.

function add_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', 'add_category_sidebars' );

With the array of arguments for retrieving categories, I can include and exclude any categories that I wish to target. Within the foreach statement, I can modify and match the layout to my overall WordPress site’s sidebar formatting.

Additionally, in my functions.php, I want to add a function to see if a sidebar exists and has a widget added to it:

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;
}

Then, within my theme’s sidebar template file, I add 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);
    }
}

WordPress Sidebars for Each Category

The result is exactly what I wanted:

WordPress Widget Sidebars for Each Category

Now, regardless of whether I add, edit, or delete categories… my sidebar areas will always be up to date!

This site uses Akismet to reduce spam. Learn how your comment data is processed.