我是wordpress插件開發的新手。在這里,我希望用戶從復選框中列出的所有注冊帖子類型中檢查他/她所需的帖子類型。下面的代碼顯示所有已注冊的帖子類型,但當用戶選中該框并保存更改時,頁面將刷新,但未選中該復選框。function fbog_options_page() { add_submenu_page( 'tools.php', 'FB Custom Open Graph', 'FB Custom Open Graph', 'manage_options', 'cpn-fbog-settings', 'cpn_fbog_options_page_html', 20 );}add_action( 'admin_menu', 'fbog_options_page' );function cpn_fbog_settings(){ register_setting('fbog_settings_group', 'fbog_settings');}add_action('admin_init', 'cpn_fbog_settings');function cpn_fbog_options_page_html(){ global $fbog_options; $fbog_options = get_option('fbog_settings'); ob_start();?> <div class="wrap"> <div class="container"> <div class="row"> <div class="col-md-12"> <center><h1>Welcome to Custom Facebook Open Graph</h1></center> <h3>Choose the post types to display the "Custom Facebook Open Graph".</h3> <form method="post" action="options.php"> <?php settings_fields('fbog_settings_group'); ?> <h4>Check the Post Types</h4> <p> <?php $post_types = get_post_types(); foreach ($post_types as $post_type) { ?> <input id="fbog_settings_<?php echo $post_type; ?>" name="fbog_settings[post_types]" type="checkbox" value="1"<?php checked( 1, $fbog_options['post_types'], false ); ?> /> <label class="description" for="fbog_settings[post_types]"><?php _e($post_type, 'fbog_domain'); ?></label> <br /> <?php } ?> </p> <p class="submit"> <input type="submit" class="button-primary" value="<?php _e('Save Options', 'fbog_domain'); ?>" /> </p> </form> </div> </div> </div> </div> <?phpecho ob_get_clean();}
1 回答

慕仙森
TA貢獻1827條經驗 獲得超8個贊
您的代碼是正確的,除非您沒有保存正確的帖子類型。
更改此行:
<input id="fbog_settings_<?php echo $post_type; ?>" name="fbog_settings[<?= $post_type ?>]" type="checkbox" value="1"<?php checked( 1, @$fbog_options[$post_type]); ?> />
- 1 回答
- 0 關注
- 81 瀏覽
添加回答
舉報
0/150
提交
取消