我试图让我的用户设置他们想要使用的小部件区域的数量。我设置了一个选项面板,让他们输入数字,然后设置我的小部件注册功能以使用 for 循环来注册该数量的小部件区域。这可行,除了每次我回到小部件管理区域时,小部件区域都是空的,不管我在其中放了什么。
这是我用来创建小部件区域的代码:
function fivepints_widgets_init() {
register_sidebar( array(
'name' => __( 'Main Sidebar', 'fivepints' ),
'id' => 'sidebar-1',
'before_widget' => '<div class="widget">',
'after_widget' => "</div>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
register_sidebar( array(
'name' => __( 'Home Sidebar', 'fivepints' ),
'id' => 'sidebar-2',
'before_widget' => '<div class="widget">',
'after_widget' => "</div>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
$fp_theme_options = get_option( 'fp_theme_options'); //get number from options panel
if ($fp_theme_options['fp_sidebars'] == ""){$num = 1;} //set number to 1 if options panel value isn't set
else {$num = $fp_theme_options['fp_sidebars']; }
for ($i=1; $i<=$num; $i++)
{
$sidebarName = 'Sidebar ' . $i;
$sidebarID = 'fp-sidebar-' . $i;
register_sidebar( array(
'name' => __( $sidebarName, 'fivepints' ),
'id' => $sidebarID,
'before_widget' => '<div class="widget">',
'after_widget' => "</div>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
}
}
add_action( 'widgets_init', 'fivepints_widgets_init' );