1

I have a small website that wants a different sidebar for each page containing blocks of text, blocks of images, and blocks of links, etc. What I'm trying to do is register a sidebar for each top level page:

if ( function_exists('register_sidebar') ){
    $topLevel = get_pages(array(
        'sort_column'   => 'post_date',
        'hierarchical'  => 0,
        'parent'        => 0,
        'post_type'     => 'page',
        'post_status'   => 'publish'
    ));

    foreach($topLevel as $page)
    {
        //make alphaunermic
        $sidebarID = preg_replace("/[^a-z0-9_\s-]/", "", strtolower($page->post_title));

        //Clean multiple dashes or whitespaces
        $sidebarID = preg_replace("/[\s-]+/", " ", strtolower($page->post_title));

        //Convert whitespaces and underscore to dash
        $sidebarID = preg_replace("/[\s_]/", "-", strtolower($page->post_title));

        register_sidebar(array(  
          'name' => $page->post_title,  
          'id'   => $sidebarID, 
          'description'   => '',  
          'before_widget' => '<div id="'.$sidebarID.'" class="widget">'  
          'after_widget'  => '</div>',  
          'before_title'  => '<h2>',  
          'after_title'   => '</h2>'  
        ));
    }
}

1) is this possible?

2) is there a better way to go about this?

3) I'm getting a few errors...

2
  • 1
    From where are you doing this and when are you doing it (on which hook)?` – kaiser Jul 25 '13 at 18:09
  • Well I'm doing it at the beginning of function.php - no hook, should I use the widgets_init hook? – Howdy_McGee Jul 25 '13 at 18:11
1

try this code for creating unique sidebar for each parent page

you can use $page->post_name(page slug) or $page->ID(page id) as sidebar id

if ( function_exists('register_sidebar') ){
    /*extract all parent pages */
    $topLevel = get_pages(array(
        'sort_column' => 'post_date',
        'hierarchical' => 0,
        'parent' => 0
    ));

    foreach($topLevel as $page){
        /* register sidebar for each parent page */
        register_sidebar(array(  
          'name' => $page->post_title,  
          'id'   => 'sidebar-'.$page->post_name, 
          'description'   => 'This widget display on page "'.$page->post_title.'"',  
          'before_widget' => '<div id="'.$sidebarID.'" class="widget">',
          'after_widget'  => '</div>',  
          'before_title'  => '<h2>',  
          'after_title'   => '</h2>'  
        ));
    }
}
1
  • Wrap this code in a function and hook it on widgets_init action. – gmazzap Jul 25 '13 at 23:29
1

If you are doing this to display different content on different pages and your sidebars are the same width (well, maybe you can change it by adding a class to the body of each page), a much simpler approach would be to just use Display Widgets and have just one sidebar:

http://wordpress.org/plugins/display-widgets/

It allows you to choose which widgets show up on which page/s.

2
  • While I agree, I'm trying to keep it as vanilla as possible, meaning little to no plugins. – Howdy_McGee Jul 25 '13 at 18:48
  • Oh, ok. Well, before I found that plugin I used to register the sidebars in the functions and then created a page template for each area of the site that needs to call a different sidebar. It was a bit more work. – mtuttle Jul 25 '13 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.