我正在制作我的主题选项,所以我正在使用WordPress Codex中的代码。我在下面使用了此代码。Creating_Options_Pages
$options = array(
array("name" => "Header Customization",
"type" => "sub-section-3",
"category" => "header-styles",
),
array("name" => "Header Image",
"desc" => "Set the image to use for the header background. ",
"id" => $shortname."_header_background_image",
"type" => "text",
"parent" => "header-styles",
"std" => ""),
array("name" => "Body Background Settings",
"type" => "sub-section-3",
"category" => "body-styles",
),
array("name" => "Body Background Color",
"desc" => "Set the color of the background on which the page is. ",
"id" => $shortname."_body_background_color",
"type" => "color-picker",
"parent" => "body-styles",
"std" => "444444"),
array("name" => "Sidebar Setup",
"type" => "sub-section-3",
"category" => "sidebar-setup",
),
array("name" => "Sidebar Position",
"id" => $shortname."_sidebar_alignment",
"type" => "radio",
"desc" => "Which side would you like your sidebar?",
"options" => array("left" => "Left", "right" => "Right"),
"parent" => "sidebar-setup",
"std" => "right"),
array("name" => "Navigation Bar Setup",
"type" => "sub-section-3",
"category" => "nav-setup",
),
array("name" => "Pages to show in Navigation Bar",
"desc" => "Select the pages you want to include. All pages are excluded by default",
"id" => $shortname."_nav_pages",
"type" => "multi-select",
"options" => mnt_get_formatted_page_array($shortname."_nav_pages"),
"parent" => "nav-setup",
"std" => "none"
),
array("name" => "Analytics",
"type" => "sub-section-3",
"category" => "analytics-setup",
),
array("name" => "Custom Google Analytics Tracking Code",
"desc" => "Enter your tracking code here for Google Analytics",
"id" => $shortname."_custom_analytics_code",
"type" => "textarea",
"parent" => "analytics-setup",
"std" => ""
),
array("name" => "category posts to show on the front page",
"desc" => "Select the category you want to include. All pages are excluded by default",
"id" => $shortname."_front_page_first_section",
"type" => "select",
"options" => mnt_get_category_array($shortname."_nav_pages"),
"parent" => "nav-setup",
"std" => mnt_get_category_array($shortname."_nav_pages")
),
array("name" => "category posts to show on the front page below",
"desc" => "Select the category you want to include. All pages are excluded by default",
"id" => $shortname."_front_page_second_section",
"type" => "select-2",
"options" => mnt_get_category_array($shortname."_nav_pages"),
"parent" => "nav-setup",
"std" => mnt_get_category_array($shortname."_nav_pages")
),
);
function create_form($options) {
echo "<form id=\'options_form\' method=\'post\' name=\'form\' >\\n";
foreach ($options as $value) {
switch ( $value[\'type\'] ) {
case "sub-section-3":
create_suf_header_3($value);
break;
case "text";
create_section_for_text($value);
break;
case "textarea":
create_section_for_textarea($value);
break;
case "multi-select":
create_section_for_multi_select($value);
break;
case "radio":
create_section_for_radio($value);
break;
case "color-picker":
create_section_for_color_picker($value);
break;
case "select":
create_section_for_category_select(\'first section\',$value);
break;
case "select-2":
create_section_for_category_select(\'second section\',$value);
break;
}
}
?>
<input name="save" type="button" value="Save" class="button" onclick="submit_form(this, document.forms[\'form\'])" />
<input name="reset_all" type="button" value="Reset to default values" class="button" onclick="submit_form(this, document.forms[\'form\'])" />
<input type="hidden" name="formaction" value="default" />
<script> function submit_form(element, form){
form[\'formaction\'].value = element.name;
form.submit();
} </script>
</form>
<?php } ?>
<?php
add_action(\'admin_menu\', \'mynewtheme_add_admin\');
function mynewtheme_add_admin() {
global $themename, $shortname, $options, $spawned_options;
if ( $_GET[\'page\'] == basename(__FILE__) ) {
if ( \'save\' == $_REQUEST[\'formaction\'] ) {
foreach ($options as $value) {
if( isset( $_REQUEST[ $value[\'id\'] ] ) ) {
update_option( $value[\'id\'], $_REQUEST[ $value[\'id\'] ] );
}
else {
delete_option( $value[\'id\'] );
}
}
foreach ($spawned_options as $value) {
if( isset( $_REQUEST[ $value[\'id\'] ] ) ) {
update_option( $value[\'id\'], $_REQUEST[ $value[\'id\'] ] );
}
else {
delete_option( $value[\'id\'] );
}
}
header("Location: themes.php?page=options.php&saved=true");
die;
}
else if(\'reset_all\' == $_REQUEST[\'formaction\']) {
foreach ($options as $value) {
delete_option( $value[\'id\'] );
}
foreach ($spawned_options as $value) {
delete_option( $value[\'id\'] );
}
header("Location: themes.php?page=options.php&".$_REQUEST[\'formaction\']."=true");
die;
}
}
add_theme_page($themename." Theme Options", "".$themename." Theme Options",
\'edit_themes\', basename(__FILE__), \'mynewtheme_admin\'); }
function mynewtheme_admin() {
global $themename, $shortname, $options, $spawned_options, $theme_name;
if ($_REQUEST[\'saved\']) {
echo \'<div id="message" class="updated fade"><p><strong>\'.$themename.\' settings saved for this page.</strong></p></div>\';
}
if ($_REQUEST[\'reset_all\']) {
echo \'<div id="message" class="updated fade"><p><strong>\'.$themename.\' settings reset.</strong></p></div>\';
}
?>
<div class="wrap">
<h1>Settings for <?php echo $themename; ?></h1>
<div class="mnt-options">
<?php
create_form($options);
?>
</div><!-- mnt-options -->
</div><!-- wrap -->
<?php } // end function mynewtheme_admin()
?>
它正常工作
So what\'s the problem
数据正在保存,但复选框和选择选项除外。
以下是codex中用于复选框和选择选项的功能代码。
create_section_for_multi_select()
在这个函数中,我没有改变任何东西。
function create_section_for_multi_select($value) {
create_opening_tag($value);
echo \'<ul class="mnt-checklist" id="\'.$value[\'id\'].\'" >\'."\\n";
foreach ($value[\'options\'] as $option_value => $option_list) {
$checked = " ";
if (get_option($value[\'id\']."_".$option_value)) {
$checked = " checked=\'checked\' ";
}
echo "<li>\\n";
echo \'<input type="checkbox" name="\'.$value[\'id\']."_".$option_value.\'" value="true" \'.$checked.\' class="depth-\'.($option_list[\'depth\']+1).\'" />\'.$option_list[\'title\']."\\n";
echo "</li>\\n";
}
echo "</ul>\\n";
create_closing_tag($value);
}
create_section_for_category_select()
在这个函数中,我改变了一些小事情,比如
post_form to oth_post_form
,
all to oth_all
和
level-0 to dr-cat-list
function create_section_for_category_select($page_section,$value) {
create_opening_tag($value);
$all_categoris=\'\';
echo \'<div class="wrap" id="\'.$value[\'id\'].\'" >\'."\\n";
echo \'<h1>Theme Options</h1> \'."\\n" .\'
<p><strong>\'.$page_section.\':</strong></p>\';
echo "<select id=\'".$value[\'id\']."\' class=\'post_form\' name=\'".$value[\'id\']."\' value=\'true\'>\\n";
echo "<option id=\'all\' value=\'\'>All</option>";
foreach ($value[\'options\'] as $option_value => $option_list) {
$checked = \' \';
echo \'value_id=\' . $value[\'id\'] .\' value_id=\' . get_option($value[\'id\']) . \' options_value=\' . $option_value;
if (get_option($value[\'id\']) == $option_value) {
$checked = \' checked="checked" \';
}
else if (get_option($value[\'id\']) === FALSE && $value[\'std\'] == $option_value){
$checked = \' checked="checked" \';
}
else {
$checked = \'\';
}
echo \'<option value="\'.$option_list[\'name\'].\'" class="level-0" \'.$checked.\' number="\'.($option_list[\'number\']).\'" />\'.$option_list[\'name\']."</option>\\n";
//$all_categoris .= $option_list[\'name\'] . \',\';
}
echo "</select>\\n </div>";
//echo \'<script>jQuery("#all").val("\'.$all_categoris.\'")</\\script>\';
create_closing_tag($value);
}
Note: The function name\'s are also changed.
最合适的回答,由SO网友:CompactCode 整理而成
我认为您没有使用customizer。php设置来添加它们,这可能会导致复杂性。
因此,您要做的是创建一个自定义程序。主题根文件夹中的php。
在那里,您需要定义不同的级别,以显示设置以及如何定义设置。(稍后将详细介绍)。然后可以使用get\\u theme\\u mod函数在布局文件中检索它们。
因此,您要做的是阅读以下内容:Theme Customizatoin ApI
因此,您需要定义面板(第一级)、节(第二级)和控件(设置本身)。如果只想在其中存储设置的1个菜单中使用面板,则可以跳过面板。
Step 1 : 创建自定义项。主题/公司/文件夹中的php
Step 2 : 创建如下函数:
function DesignitMultistore_customize_register($wp_customize)
{
// here comes your code to define panels , sections and controls and remove them
}
add_action(\'customize_register\', \'DesignitMultistore_customize_register\');
How to remove default controls :
$wp_customize->remove_control(\'header_textcolor\');
$wp_customize->remove_control(\'display_header_text\');
$wp_customize->remove_control(\'blogdescription\');
如何添加面板示例:
// Slider Panel
$wp_customize->add_panel(\'Designit_slider\', array(
\'title\' => __(\'Slider\', \'DesignitMultistore\'),
\'description\' => sprintf(__(\'Settings for slider on Homepage. Here you can upload up to 3 images with text to display on the home page.\', \'DesignitMultistore\')),
\'priority\' => 150,
));
注意:面板和剖面仅在控件就位时显示。
How to add sections :
// Add General settings
$wp_customize->add_section(\'Designit_slider_general\', array(
\'title\' => __(\'General settings\', \'DesignitMultistore\'),
\'description\' => sprintf(__(\'Setup up your General settings\', \'DesignitMultistore\')),
\'priority\' => 1,
\'panel\' => \'Designit_slider\',
));
一
一六//常规设置:滑块收音机复选框
$wp_customize->add_setting(\'slider_radio\', array(
\'default\' => \'slider\',
\'sanitize_callback\' => \'designit_slider_radio_sanitizer\',
));
$wp_customize->add_control(\'slider_radio\', array(
\'type\' => \'radio\',
\'description\' => __(\'Slider : Use the slider on the home page. Header image : Use the header image on the home page.\', \'DesignitMultistore\'),
\'label\' => __(\'Enable slider\', \'DesignitMultistore\'),
\'section\' => \'Designit_slider_general\',
\'priority\' => 1,
\'choices\' => array(
\'slider\' => __(\'Slider\', \'DesignitMultistore\'),
\'static\' => __(\'Header Image\', \'DesignitMultistore\'),
),
));
note : 每个控件都需要一个设置,因此此处的两个部分构成一个控件。sanitize\\u回调用于在数据发送到数据库之前检查数据。您可以使用wordpress中的一个对其进行清理,或在同一自定义中创建一个函数。php,您可以这样定义它们:
//sanitizer switch
function designit_fade_radio_sanitizer($input)
{
$valid = array(
\'fade\' => __(\'Fade\', \'DesignitMultistore\'),
\'slide\' => __(\'Slidee\', \'DesignitMultistore\'),
);
if (array_key_exists($input, $valid)) {
return $input;
} else {
return \'\';
}
}
就这样!现在,您可以使用调用前端中的设置
get_theme_mod(\'slider_radio\', \'slider\')
;