我实际上能够让它工作。对我来说,关键是确保 register_setting() 对组使用相同的 slug,即使部分不同。本质上,我将第一部分的 slug 用作第一个参数,而不是每个部分的 slug。在我的设置中,这是使用我所谓的“页面”变量,因为我用它来定义选项页面。
我的部分和字段定义如下:
$sections=>array(
...
'page_vars'=>array(
'slug'=>'page_vars',
'label'=>'Page Layout Variables',
),
'display_defaults'=>array(
'slug'=>'display_defaults',
'label'=>'Display Defaults',
'page' =>'page_vars',
),
'blog_defaults'=>array(
'slug'=>'blog_defaults',
'label'=>'Blog Display Defaults',
'page' =>'page_vars',
),
...
);
对应的字段:
$fields=>arry(
....
'max_page_width'=>array(
'slug'=>'max_page_width',
'label'=>'Maximum width for page (Including header images - include unit)',
'section'=>'page_vars',
'args'=>array('default'=>'100vw'),
),
'xl_max_body_width'=>array(
'slug'=>'xl_max_body_width',
'label'=>'Default content width for XL Screens (include unit)',
'section'=>'page_vars',
'args'=>array('default'=>'1600px'),
),
'lg_max_body_width'=>array(
'slug'=>'lg_max_body_width',
'label'=>'Default content width for LG Screens (include unit)',
'section'=>'page_vars',
'args'=>array('default'=>'95vw'),
),
'md_max_body_width'=>array(
'slug'=>'md_max_body_width',
'label'=>'Default content width for MD Screens (include unit)',
'section'=>'page_vars',
'args'=>array('default'=>'95vw'),
),
'sm_max_body_width'=>array(
'slug'=>'sm_max_body_width',
'label'=>'Default content width for SM Screens (include unit)',
'section'=>'page_vars',
'args'=>array('default'=>'95vw'),
),
'xs_max_body_width'=>array(
'slug'=>'xs_max_body_width',
'label'=>'Default content width for XS Screens (include unit)',
'section'=>'page_vars',
'args'=>array('default'=>'95vw'),
),
'display_header'=>array(
'slug'=>'display_header',
'label'=>'Display auto-generated page heading by default',
'section'=>'display_defaults',
'page' =>'page_vars',
),
'display_title'=>array(
'slug'=>'display_title',
'label'=>'Display auto-generated page title by default',
'section'=>'display_defaults',
'page' =>'page_vars',
),
'display_meta'=>array(
'slug'=>'display_meta',
'label'=>'Display auto-generated page meta by default',
'section'=>'display_defaults',
'page' =>'page_vars',
),
'display_thumb'=>array(
'slug'=>'display_thumb',
'label'=>'Display thumbnail at top of post',
'section'=>'display_defaults',
'page' =>'page_vars',
),
'display_page_comments'=>array(
'slug'=>'display_page_comments',
'label'=>'Display comments on pages',
'section'=>'display_defaults',
'page' =>'page_vars',
),
'display_post_comments'=>array(
'slug'=>'display_post_comments',
'label'=>'Display comments on posts',
'section'=>'display_defaults',
'page' =>'page_vars',
),
'display_sidebar'=>array(
'slug'=>'display_sidebar',
'label'=>'Display sidebar by default',
'section'=>'display_defaults',
'page' =>'page_vars',
),
'mobile_sidebar'=>array(
'slug'=>'mobile_sidebar',
'label'=>'Display sidebar on Mobile Devices',
'section'=>'display_defaults',
'page' =>'page_vars',
),
'display_footer'=>array(
'slug'=>'display_footer',
'label'=>'Display footer by default',
'section'=>'display_defaults',
'page' =>'page_vars',
),
'blog_display_header'=>array(
'slug'=>'blog_display_header',
'label'=>'Display auto-generated page heading by default',
'section'=>'blog_defaults',
'page' =>'page_vars',
),
'blog_display_title'=>array(
'slug'=>'blog_display_title',
'label'=>'Display auto-generated page title by default',
'section'=>'blog_defaults',
'page' =>'page_vars',
),
'blog_display_filter'=>array(
'slug'=>'blog_display_filter',
'label'=>'Display post filter above content',
'section'=>'blog_defaults',
'page' =>'page_vars',
),
...
);
然后我添加了一些处理函数来添加/注册部分和字段:
// set up options record in database
foreach ($sections as $section=>$def){
$page = (isset($def['page']))? $def['page'] : $def['slug'];
add_settings_section(
'trs_harvest_'.$def['slug'],
__('','trs-harvest'),
array($this,$def['slug'].'_settings_cb'),
'trs_harvest_'.$page
);
}
foreach ($fields as $field=>$def){
$page = (isset($def['page']))? $def['page'] : $def['section'];
$args = (isset($def['args']))? $def['args'] :null;
register_setting('trs_harvest_'.$page,'hvst_'.$def['slug'],$args);
add_settings_field(
'hvst_'.$def['slug'],
__($def['label'],'trs-harvest'),
array($this,$def['slug'].'_cb'),
'trs_harvest_'.$page,
'trs_harvest_'.$def['section'],
array('label_for'=>'hvst_'.$def['slug'])
);
}
最后,当我添加选项页面时,我确保 settings_fields() 使用了第一部分(*重要说明是第一部分驱动这里的 nonce 字段)并且只创建了一组 settings_field() 和 _submit_button() 输出这页纸。:
将其用作 $sections:
array('trs_harvest_page_vars','trs_harvest_display_defaults','trs_harvest_blog_defaults')
以及此代码来设置选项页面输出:
settings_fields( $sections[0]);
foreach ($sections as $section){
do_settings_sections( $section);
}
echo('</div>');
submit_button('Save Settings');
这给了我一个包含多个部分的选项页面,我可以使用单个提交按钮保存这些部分。