【发布时间】:2014-08-24 08:50:20
【问题描述】:
我正在尝试学习如何在 WordPress 中制作自定义仪表板页面和设置。我遵循的教程很清楚......但是有一件非常小的事情对我来说没有意义。
在我的 funcions.php 底部附近的demo_footer_message_display() 函数中有一行代码,上面写着option['message']。我不明白“消息”是在哪里定义的。如果它说 option['footer_message'] 会很有意义,因为'footer_message' 是我正在处理的字段的 id。
代码运行良好,但我就是不明白为什么? “消息”在哪里被定义为选项数组的索引。谁能给我解释一下?
PS。如果不是很明显我的设置页面只有一个输入,您可以在主题的页脚中输入一些文本。
functions.php
<?php /* --------------------------------------------* * Menus /*---------------------------------------------*/ // Adds the 'DEMO THEME OPTIONS' to the 'Settings' menu in WordPress function demo_add_options_page(){ add_options_page( 'Lindsay Demo Theme Options', //browser title 'Lindsay Demo Theme Options', // menu text 'manage_options', // required capability of users to access this menu 'lindsay-demo-theme-options', // slug 'demo_theme_option_display' // name of function used to display content ); } add_action('admin_menu', 'demo_add_options_page'); /* --------------------------------------------* * Sections, Settings, and Fields /*---------------------------------------------*/ // Registers a new settings field on the 'Demo Theme Options' page function demo_initialize_theme_options(){ //section to be rendered on the new options page add_settings_section( 'footer_section', //id 'Footer Options', //title on screen 'demo_footer_options_display', //callback 'lindsay-demo-theme-options'//ID of page ); //define the settings field add_settings_field( 'footer_message', //ID of field 'Theme Footer Message', //label 'demo_footer_message_display', //callback 'lindsay-demo-theme-options', // page 'footer_section' // the section to add to ); //Register the 'footer_message' setting with the 'General' section register_setting( 'footer_section', //name of the group of settings 'footer_options' //name of option ); } //end demo_initialize_theme_options add_action('admin_init', 'demo_initialize_theme_options'); /* --------------------------------------------* * Callbacks /*---------------------------------------------*/ function demo_theme_option_display(){ ?> <div class="wrap"> <h2 >Demo Theme Options</h2> <form method="post" action="options.php"> <?php // render the settings for the settings section identified as 'Footer section' settings_fields('footer_section'); //render all of the settings for 'demo-theme-options' sections do_settings_sections('lindsay-demo-theme-options'); // add the submit button to serialize options submit_button(); ?> </form> </div> <?php } function demo_footer_message_display(){ $options = (array)get_option('footer_options'); $message = $options['message']; echo '<input type="text" name="footer_options[message]" id="footer_options_message" value="'.$message.'"/>'; } function demo_footer_options_display(){ echo 'These options are designed to help you control whats dislayed in your footer'; }
footer.php
<div id="footer" class="col-md-12">
<?php wp_footer() ?>
<div class="site-info">
<a href="http://wordpress.org/" title="A Semantic Personal Publishing Platform" rel="generator">Proudly Powered by WordPress</a>
<span class="sep"> | </span>
<?php $options = (array) get_option('footer_options');?>
<?php $message = $options['message'];?>
<span id="footer-message"><?php echo $message; ?></span>
</div>
</div>
【问题讨论】: