wordpress多选在主题选项页面

时间:2017-10-10 09:05:35

标签: php wordpress select

我试图让这项工作成功。它的主题选项页面列出了类别并允许您进行MULTIPLE选择 但有些事情是错误的,它没有保存价值,最后我如何在索引中显示这些选定的类别?

这是我在网络和沙发中找到的内容

<?php
add_action( 'admin_init', 'theme_options_init' );
add_action( 'admin_menu', 'theme_options_add_page' );
function theme_options_init(){
    register_setting( 'sample_options', 'sample_theme_options', 'theme_options_validate' );
}
function theme_options_add_page() {
    add_theme_page( __( 'Theme Options', 'sampletheme' ), __( 'Theme Options', 'sampletheme' ), 'edit_theme_options', 'theme_options', 'theme_options_do_page' );
}
function theme_options_do_page() {
    global $select_options, $radio_options;
    if ( ! isset( $_REQUEST['settings-updated'] ) )
        $_REQUEST['settings-updated'] = false;
    ?>
    <div class="wrap">
        <?php screen_icon(); echo "<h2>" . get_current_theme() . __( ' Theme Options', 'sampletheme' ) . "</h2>"; ?>
        <?php if ( false !== $_REQUEST['settings-updated'] ) : ?>
        <div class="updated fade"><p><strong><?php _e( 'Options saved', 'sampletheme' ); ?></strong></p></div>
        <?php endif; ?>
        <form method="post" action="options.php">
            <?php settings_fields( 'sample_options' ); ?>
            <?php $options = get_option( 'sample_theme_options' ); ?>
<select multiple="multiple" name="site_options[categorychoice][]">
   <?php $option = get_option('site_options'); ?>
   <?php
   $args = array(
     'orderby' => 'name',
     'parent' => 0,
     'exclude' => 1
   );
   $categories = get_categories( $args );
   foreach ($categories as $category) { ?>
   <?php $selected = in_array( $category->cat_ID, $option['categorychoice'] ) ? ' selected="selected" ' : ''; ?>
       <option value="<?php echo $category->term_id; ?>" <?php echo $selected; ?> >
          <?php echo $category->cat_name . ' (' . $category->category_count .')'; ?>
       </option>
   <?php } //endforeach ?>
</select>
            <p class="submit">
                <input type="submit" class="button-primary" value="<?php _e( 'Save Options', 'sampletheme' ); ?>" />
            </p>
        </form>
    </div>
    <?php
}
function theme_options_validate( $input ) {
    global $select_options, $radio_options, $categories;
    if ( ! array_key_exists( $input['categorychoice'], $categories ) )
        $input['categorychoice'] = NULL;
    return $input;
}

0 个答案:

没有答案