Support » Fixing WordPress » How do I register one sidebar for widgets per category?

  • Resolved bechster

    (@bechster)


    Hi,

    First of all, sorry for cross posting.

    I often find myself in need of custom widgets or boxes of some sort for displaying related information on a per post/page basis.

    Specifically, I’m trying to create a sidebar per category for displaying widgets with related content for each category in my category archive.

    I have tried the following in functions.php but it doesn’t seem to work. However, I can’t figure out why it doesn’t. Does anyone have any ideas?

    <?php
      $categories = get_categories('orderby=name&order=asc');
      foreach ($categories as $category) {
        register_sidebar(array(
          'name' => $category->cat_name,
          'id' => $category->category_nicename,
          'before_widget' => '<div id="%1$s" class="%2$s widget">',
          'after_widget' => '</div>',
          'before_title' => '<h3 class="widget-title">',
          'after_title' => '</h3>'
        ));
      }
    ?>

    Any help is much appreciated.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Have you tried Slayers Custom Widget

    shttp://www.thaslayer.com/2008/11/08/slayers-custom-widgets-v12-wordpress-plugin-finally-out/

    It allows you to decide which category you like to display which widget without the need to write any code, i’ve tested and seems to work quite well.

    Sorry if i’ve misunderstood your question and this is not quite qht your after.

    Thank you, interesting plugin. It may come in handy.

    However, I need a custom sidebar for each category archive and although this plugin does deliver some flexibility, it doesn’t quite do the trick.

    Also, I’m trying to build the functionality into a theme so plugins are no-go in the long run (but they’re great for inspiration).

    bechster

    (@bechster)

    Got it! Hope this solution may be useful for others as well. But if you have any improvements, please post it. For instance, I could use an ID on each widget. Perhaps it is possible to use the category slug as the ID, but I am yet to find a solution for that.

    Here goes the code:

    <?php
    // Get sidebar names from db.
    global $wpdb;
    $my_widget_name = $wpdb->get_col("SELECT name FROM $wpdb->terms, $wpdb->term_taxonomy WHERE $wpdb->terms.term_id=term_taxonomy_id AND taxonomy='category' AND count!=0 ORDER BY name ASC");
    
    // Register one sidebar per category name.
    foreach($my_widget_name as $my_widget) {
      register_sidebar(array(
        'name' => 'Category: ' . $my_widget,
        'before_widget' => '<div id="%1$s" class="%2$s widget">',
        'after_widget' => '</div>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>', ));
    }
    ?>
    daerchwing

    (@daerchwing)

    Thanks, I can see that coming in real handy.

    Another solution
    > one sidebar per category
    > and one sidebar per page

    if(function_exists('register_sidebar')) {
        foreach((array)(get_categories()) as $category) {
      register_sidebar(array(
    		'name' => 'Categorie : ' .$category->cat_name,
        'before_widget' => '<div id="%1$s" class="%2$s widget">',
        'after_widget' => '</div>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>', ));
    } 
    
        foreach((array)(get_pages()) as $page) {
      register_sidebar(array(
    		'name' => 'Page : ' .$page->post_name,
        'before_widget' => '<div id="%1$s" class="%2$s widget">',
        'after_widget' => '</div>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>', ));
    }
    }

    S├ębastien – wordpressdesigner – France

    Hello Daerchwing and Archiparmentier. I’ve been searching for the same solution and google lead me to you guys!

    I just need a little more info as to how to implement your code.

    When I register new sidebars per category or page (Archiparmentier), how do I name them exactly? Will their widgetized areas show up in my widgets admin?

    Thanks, this is going to help out a lot!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How do I register one sidebar for widgets per category?’ is closed to new replies.