<?php
//From https://wordpress.org/support/topic/adding-a-sidebar-to-subcategory-archives/#post-10209291
// Filter Custom Sidebars to include sidebar in subcategory archives
add_filter( 'custom_sidebars_set_location', function( $options, $id, $sidebars, $data ){
// Go through all registered custom sidebars, category id as index
foreach ( $options['category_archive'] as $cat_id => $sidebar ) {
// If the looped category has no sidebar set, just skip it to save time
if ( empty( $sidebar ) )
continue;
// Go through all the category children
$children = get_term_children( $cat_id, 'category' );
foreach ( $children as $child ) {
// If the child category has no sidebar set to it, copy the parent sidebar
if ( empty( $options['category_archive'][$child] ) ) {
$options['category_archive'][$child] = $sidebar;
}
}
}
return $options;
}, 10, 4 );