我有主题选项/设置页面和选项/设置字段,如下所示:http://code.tutsplus.com/tutorials/the-complete-guide-to-the-wordpress-settings-api-part-2-sections-fields-and-settings--wp-24619
我想知道的是,如何register 1)小部件和2)基于特定主题选项/设置字段的菜单。注意:我想\'register\' 菜单/小部件不仅仅显示它。逻辑是,在后端,用户可能会因为有多余的小部件/菜单而感到困惑。
具体来说,我使用主题设置为管理员用户提供了设置不同标题类型的能力,我们可以将它们称为“1”、“2”和“3”
标题中的代码。php文件是(我正在通过“get\\u template\\u part”提取模板部分):
<?php if($display_options[\'header_layout\'] == \'1\'): ?>
<?php get_template_part( \'partials/header\', \'left\' ); ?>
<?php endif; ?>
<?php if($display_options[\'header_layout\'] == \'2\'): ?>
<?php get_template_part( \'partials/header\', \'center\' ); ?>
<?php endif; ?>
<?php if($display_options[\'header_layout\'] == \'2\'): ?>
<?php get_template_part( \'partials/header\', \'right\' ); ?>
<?php endif; ?>
如上所述,上述代码[header\\u layout]使用设置字段中的三个选项之一。基于此字段,我正在加载不同的模板部分,理想情况下,每个模板部分都有不同的菜单/小部件。
我挂断电话的地方是尝试有条件地注册菜单/小部件,例如(这在一个单独的widgets.php文件中):
function arphabet_widgets_init() {
if($display_options[\'header_layout\'] == \'2\') {
register_sidebar( array(
\'name\' => \'Header Widget 2\',
\'id\' => \'widget_header_2\',
\'before_widget\' => \'<div class="nav widget">\',
\'after_widget\' => \'</div>\',
\'before_title\' => \'<h2 class="widgettitle">\',
\'after_title\' => \'</h2>\',
) );
};
}
add_action( \'widgets_init\', \'arphabet_widgets_init\' );
我认为我从$display\\u options传递变量的方式是错误的。如果需要,非常乐意澄清,并提前感谢您的任何想法!
SO网友:Chris
下面是您需要的代码。您的问题可能是您没有首先设置变量$display\\u options来包含您的选项,或者您没有询问是否设置了该选项。
add_action(\'widgets_init\', \'arphabet_widgets_init\');
function arphabet_widgets_init() {
$options = get_option(\'muffin_options\');
if(isset($display_options[\'header_layout\']) && $display_options[\'header_layout\'] == \'1\') {
register_widget(\'arphabet_widget_1\');
}
if(isset($display_options[\'header_layout\']) && $display_options[\'header_layout\'] == \'1\') {
register_sidebar( array(
\'name\' => \'Header Widget 2\',
\'id\' => \'widget_header_2\',
\'before_widget\' => \'<div class="nav widget">\',
\'after_widget\' => \'</div>\',
\'before_title\' => \'<h2 class="widgettitle">\',
\'after_title\' => \'</h2>\',
) );
}
}
此外,值得注意的是,您可以为您的选项设置默认设置。这避免了每次使用时都需要询问是否设置了选项设置,因此不需要使用
isset($display_options[\'show_my_menu\'])
.
要做到这一点,请修改以下代码-为了清晰起见,包含了整个代码。
add_action( \'admin_init\', \'arphabet_options_init\' );
function arphabet_options_init() {
register_setting( \'arphabet_options\', \'arphabet_options\' ); // Register your setting
arphabet_options_defaults(); // Set up defaults in the function named \'arphabet_options_defaults\'
}
function arphabet_options_defaults() {
$update_options= get_option(\'arphabet_options\'); // Store options array data in a variable
$update_options[\'show_my_widget\'] = true; // Add this key/value to the $update_options array: \'show_my_widget\' => true
$update_options[\'show_my_menu\'] = true; // Same as above, but for show_my_menu
add_option(\'arphabet_options\', $display_options); // Add these settings to your option, if they don\'t already exists (use update_option to replace settings)
}
值得注意的部分是add\\u选项。这将$display\\u options中的键/值对与arphabet\\u options选项中已存储的数组数据合并,基本上,它会将设置添加到数据库中。尽管如此,If不会替换现有设置;对此使用update\\u选项。
如果您想了解有关使用设置/更新选项的更多信息,请查看下面的链接,尽管它们并没有真正涉及使用带选项的数组。因此,我创建了一个Pastebin,其中显示了我经常使用的一些有用的片段。
http://pastebin.com/NM4Xk4BR
https://codex.wordpress.org/Function_Reference/add_optionhttps://codex.wordpress.org/Function_Reference/update_option