• Resolved purtonen

    (@purtonen)


    Hi,

    I’m having an issue with displaying a sidebar on my subcategory archive page.

    What I would like is to add a sidebar to the parent category archive page and then have it show up on all the subcategories as well. The subcategories vary a lot and they are constantly being added and removed, so selecting all of them in the sidebar location options is not really an option here.

    From the plugin code (since I can’t even find any documentation on filters/actions etc.) I found that taxonomies and post types could be filtered with cs_replace_post_type and cs_replace_taxonomy but no such luck on categories.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hello @purtonen,

    hope you’re doing good today! ??

    Unfortunately this is happening by design. There’s a way to force a sidebar to be used for all Category Archives though, could that work for you?

    Warm regards,
    Dimitris

    Thread Starter purtonen

    (@purtonen)

    I have at least three different sidebars to show on three different categories, so forcing one on all of them isn’t going to work for me.

    Thread Starter purtonen

    (@purtonen)

    Ok I found out this myself, the below code pretty much works as I intended:

    
    // 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 );
    

    This applies a filter to the sidebar options when saving the sidebar in wp-admin. The filter goes through the category_archive sidebars, then all the subcategories of that category. If the subcategory has no sidebars set, it copies the parent category sidebar to it.

    So now in the admin side, if I add a category archive to the sidebar location and save it, this filter adds all the subcategories to that list aswell.

    Hello @purtonen,

    I’m really glad for both finding this out and sharing your solution! ??

    Warm regards,
    Dimitris

    Thanks for sharing your solution. I’ll be using this as well!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Adding a sidebar to subcategory archives’ is closed to new replies.