WordPress.org

Ready to get started?Download WordPress

Forums

Multiple dynamic sidebars based on category (1 post)

  1. Tecktron
    Member
    Posted 5 years ago #

    This may be beyond the scope of wordpress (I hope not), but here goes.

    I need to dynamically have sidebars created each time a new category is added, well sort of. Why? Well each category needs to have it's own set of widgets. I have no idea how many widgets or how many categories there will be. I'm trying to limit the amount of editing that needs to be done each time a new category is added (I'm guessing once a month). It would also be nice to have this so it's all laid out nicely for the user in the admin panel as well. I'm a bit new to the wordpress api, though I am a seasoned developer and thinking that maybe WP not the right choice for what's required here but this is the only reason why so far and I'm thinking there has to be some way around it and I just need to be pointed in the right direction, and not make that left at Albuquerque.

    So here's what I've tried.

    I've tried this in functions.php

    $categories = get_categories(
     array(
        'type' => 'post',
        'child_of' => 0,
        'orderby' => 'id',
        'order' => 'ASC',
        'hide_empty' => true,
        'include_last_update_time' => false,
        'hierarchical' => 0,
        'pad_counts' => false
     )
    );
    foreach ($categories as $cat) {
     register_sidebar(
       array(
            'name' => $cat->slug.'-Sidebar',
    	'before_widget' => '<div class="sidebarContent">',
    	'after_widget' => '</div>',
            'before_title'  => '<h3>',
    	'after_title'   => '</h3>'
        )
     );
    }

    And then in sidebars.php I have this:

    if(is_category()){
    	$category = end(get_the_category());
     	dynamic_sidebar($category->slug.'-Sidebar');
    }

    This results in getting 2 sidebars with -Sidebar as the name in the admin panel (even through I've only got one category) and neither of them work and nothing is displayed on the category page. So I guessing that I'm doing something wrong.

    I have checked out the Advanced Text Widget and I find it quite useful until I come fore-thinking the naming of the same widgets, for example one named News for category a and another named News for category b then c, d, and so on on and so forth, then it's a needle in a haystack finding the right one to edit, which then become a real mess.

    Maybe there is a better WP solution or something, I don't know. Any help, insight or input would be appreciated. I will of course add anything I come up with to this post for anybody's future reference.

    Thanks Much!
    --T

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.