我希望添加一个设置字段,如page_for_posts
在设置区域中的某个位置添加以下代码functions.php
. 这将定义page_for_search
选项,轻松获取价值get_option(\'page_for_search\')
作用
add_action(\'admin_init\', \'my_custom_page_settings\');
function my_custom_page_settings() {
add_settings_section(
\'custom_page_settings\',
\'Custom Page Settings\',
\'custom_page_settings_helper_text\',
\'reading\'
);
add_settings_field(
\'page_for_search\',
\'Page For Search\',
\'page_for_search_callback\',
\'reading\',
\'custom_page_settings\',
array(
\'page_for_search\'
)
);
register_setting(\'reading\',\'page_for_search\', \'esc_attr\');
}
function custom_page_settings_helper_text() {
echo \'<p>You can define default pages below.</p>\';
}
function page_for_search_callback($args) {
echo wp_dropdown_pages( array( \'name\' => $args[0], \'echo\' => 0, \'show_option_none\' => __( \'— Select —\' ), \'option_none_value\' => \'0\', \'selected\' => get_option( \'page_for_search\' ) ) );
}
您的客户端将能够在设置->阅读页面中定义搜索页面,您可以轻松地在搜索中添加字段。php模板如下:
<?php $search_page_id = get_option(\'page_for_search\'); ?>
<h1><?php echo get_the_title($search_page_id); ?></h1>
<div class="content"><?php echo get_post_field(\'post_content\', $search_page_id); ?></div>
您可以阅读有关设置api的更多信息
here.