0

I have the following code in my functions.php

if ( function_exists('register_sidebars') ){

    $pages=get_pages();

    foreach($pages as $page){

        register_sidebar(array('name'=>$page->post_title, 'sort_column'=>'ID','sort_order'=>'ASC', 'id'=>$page->ID));

    }
}

It works beautifully to give me a sidebar for each page. The problem is that the sidebar contest are constantly being reset. Every time I go back to my widgets page, the sidebars are all blank. I can usually still see the contents on the actual site, unless I update something in a different sidebar. If I take out the loop and just use register_sidebar to register a single sidebar, everything seems to work fine. Any ideas why?

1

You need to wrap this in a function then add it to the widgets_init action. Also the register sidebar function does not have $arguments for sort_column or sort_order. I would also change the id to 'sidebar-'.$page->ID

add_action( 'widgets_init', 'prefix_register_sidebars' );
function prefix_register_sidebars() {
    foreach($pages as $page){
        register_sidebar( array(
        'name'=>$page->post_title, 
        'id'=> 'sidebar-'.$page->ID,
        'before_widget'   => '<div id="%1$s" class="'.$page->slug.' widget %2$s">',
        'after_widget'    => '</div>',
        'before_title'    => '<h3>',
        'after_title'     => '</h3>'
        )
        ); 
}
  • I had tried using widgets_init, but got the same results. Unfortunately, widgets_init is pretty poorly documented (IMO). I think I fixed it by using 'sidebar-'.$page->ID. I found another blog where someone had trouble using uppercase letters, and I noticed the codex says that ids "Must be all in lowercase" so that probably was the issue. Things seem to be working properly now, but sometimes there has been a delay before things get reset so I will wait 24 hours and then mark your answer as correct if everything is still good. – Dustin Dec 12 '11 at 5:18

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.