类别列表,而不是管理选项中的下拉菜单选择器

时间:2012-02-21 作者:Marion

我在管理区域中使用以下代码theme options

$categories = get_categories(\'hide_empty=0&orderby=name\');  
$tz_wp_cats = array();  
foreach ($categories as $category_list ) {  
    $tz_wp_cats[$category_list->cat_ID] = $category_list->cat_name;  
    }  
array_unshift($tz_wp_cats, "Choose a category");

// ==========================//
//             Start the theme options!                  //
// ==========================//

    $themename = "Hype";
    $shortname = "tz";

    $options = array (

    array(  "name" => __("selected", \'framework\'),
    "id" => $shortname."_selectedtab",
    "std" => "",
    "type" => "hidden"),

    array(  "type" => "opentab"),

    array(  "type" => "open"),


    array(  "name" => __(\'Logo and Favicon Settings\', \'framework\'),
    "id" => $shortname."_logo_settings",
    "type" => "title"),

    array(  "name" => __("Upload Logo", \'framework\'),
    "desc" => __("Enter the full URL of an image you would like to use as a logo e.g http://www.example.com/logo.png", \'framework\'),
    "id" => $shortname."_logo_url",
    "std" => "",
    "type" => "file"),

    array(  "name" => __("Enable plain text logo",\'framework\'),
    "desc" => __("Check this box to use a plain text logo rather than an image. Info will be taken from your WordPress settings.", \'framework\'),
    "id" => $shortname."_plain_logo",
    "std" => "false",
    "type" => "checkbox"),

    array(  "name" => __("Favicon URL", \'framework\'),
    "desc" => __("Enter the full URL of your favicon e.g. http://www.example.com/favicon.ico", \'framework\'),
    "id" => $shortname."_favicon_url",
    "std" => get_bloginfo(\'template_directory\')."/favicon.ico",
    "type" => "file"),

    array(  "type" => "close"),

    array(  "type" => "closetab")

    );

    /*Add The Theme Options Page*/
    function mytheme_add_admin() {

    global $themename, $shortname, $options;

    if ( $_GET[\'page\'] == basename(__FILE__) ) {

        if ( \'save\' == $_REQUEST[\'action\'] ) {

                $url = $_REQUEST[\'tz_selectedtab\'];
                if ($url == \'\'){
                    $url = \'themes.php?page=theme-options.php&saved=true&tab=1\';
                } else {
                    $t = substr($url, -1);
                    $url = \'themes.php?page=theme-options.php&saved=true&tab=\'.$t;
                }

                foreach ($options as $value) {
                    if ( ($value[\'id\'] != \'tz_logo_url\') && ($value[\'id\'] != \'tz_favicon_url\') ){
                        update_option( $value[\'id\'], $_REQUEST[ $value[\'id\'] ] ); 
                    }   
                }

                foreach ($options as $value) {
                    if ( ($value[\'id\'] != \'tz_logo_url\') && ($value[\'id\'] != \'tz_favicon_url\') ){
                        if( isset( $_REQUEST[ $value[\'id\'] ] ) ) { update_option( $value[\'id\'], $_REQUEST[ $value[\'id\'] ]  ); } else { delete_option( $value[\'id\'] ); } 
                    }
                }


            // If files has been uploaded, move them to the /uploads dir and update the option value
            if (  (isset($_FILES[\'tz_logo_url\'])) && ($_FILES[\'tz_logo_url\'][\'error\'] == UPLOAD_ERR_OK)  ) {
                $overrides = array(\'test_form\' => false); 
                $file = wp_handle_upload($_FILES[\'tz_logo_url\'], $overrides);
                $urlimage = $file[\'url\'];
                update_option(\'tz_logo_url\', $urlimage);
            }

            if (  (isset($_FILES[\'tz_favicon_url\'])) && ($_FILES[\'tz_favicon_url\'][\'error\'] == UPLOAD_ERR_OK)  ) {
                $overrides = array(\'test_form\' => false); 
                $file = wp_handle_upload($_FILES[\'tz_favicon_url\'], $overrides);
                $urlimage = $file[\'url\'];
                update_option(\'tz_favicon_url\', $urlimage);
            }

                header("Location: ".$url);
                die;

        } else if( \'reset\' == $_REQUEST[\'action\'] ) {

            foreach ($options as $value) {
                delete_option( $value[\'id\'] ); }

            header("Location: themes.php?page=theme-options.php&reset=true");
            die;

        }
    }

    add_theme_page($themename." Options", "Options", \'edit_themes\', basename(__FILE__), \'mytheme_admin\');
    }

    function mytheme_admin() {

    global $themename, $shortname, $options;

    if ( $_REQUEST[\'saved\'] ) echo \'<div id="message" class="updated fade"><p><strong>\'.$themename.\' settings saved.</strong></p></div>\';
    if ( $_REQUEST[\'reset\'] ) echo \'<div id="message" class="updated fade"><p><strong>\'.$themename.\' settings reset.</strong></p></div>\';

?>
        <?php // now style the actual theme options page ?>

        <div class="wrap">
            <div id="icon-themes" class="icon32"><br /></div>
            <h2><?php _e(\'Theme Options\', \'framework\') ?></h2>

            <form method="post" action="" enctype="multipart/form-data" style="overflow:hidden;">
            <input type="hidden" name="selectedtab" id="selectedtab" value="0"/>    

            <div id="tabs" class="metabox-holder clearfix">
                <ul id="tab-items">
                <li><a href="#tabs-1"><?php _e(\'General\', \'framework\') ?></a></li>
                <li><a href="#tabs-2"><?php _e(\'Navigation\', \'framework\') ?></a></li>
                <li><a href="#tabs-3"><?php _e(\'Homepage\', \'framework\') ?></a></li>
                <li><a href="#tabs-4"><?php _e(\'Post Settings\', \'framework\') ?></a></li>
                </ul>
                <div class="clear"></div>


                <div class="postbox-container">

                <?php 
                $tab = 1;
                foreach ($options as $value) { 
                switch ( $value[\'type\'] ) {

                case "opentab": ?>
                <div id="tabs-<?php echo $tab;?>">
                <?php 
                $tab++;
                break;

                case "closetab": ?>
                </div><!-- #tabs- -->
                <?php
                break;

                case "open": // style the opening
                ?>
                <!-- div id="tabs-<?php echo $tab;?>" -->
                <div class="postbox">

                <?php 
                break;

                case "note": // style the notes
                ?>
                <div class="notes"><p><?php echo $value[\'desc\']; ?></p></div>

                <?php 
                break;

                case "title": // style the titles
                ?>
                <h3 class="hndle"><span><?php echo $value[\'name\']; ?></span></h3>
                <div class="inside">        

                <?php break;

                case "hidden": ?>

                <input name="<?php echo $value[\'id\']; ?>" id="<?php echo $value[\'id\']; ?>" type="hidden" value="<?php if ( get_settings( $value[\'id\'] ) != "") { echo stripslashes(htmlspecialchars(get_settings( $value[\'id\'] ), ENT_QUOTES)); } else { echo $value[\'std\']; } ?>" />


                <?php break;

                case "text": // style the text boxes
                ?>

                <div class="textcont">
                    <div class="value">
                        <?php echo $value[\'name\']; ?>:
                    </div>
                    <div class="input">
                        <input name="<?php echo $value[\'id\']; ?>" id="<?php echo $value[\'id\']; ?>" type="<?php echo $value[\'type\']; ?>" value="<?php if ( get_settings( $value[\'id\'] ) != "") { echo stripslashes(htmlspecialchars(get_settings( $value[\'id\'] ), ENT_QUOTES)); } else { echo $value[\'std\']; } ?>" />
                        <p><?php echo stripslashes(htmlspecialchars($value[\'desc\'])); ?></p>
                    </div>
                    <div class="clear"></div>
                </div>

                <?php break;

                case "colour": // style the text boxes
                ?>

                <script type="text/javascript">
                jQuery(function() {

                    jQuery(\'#colourpicker-<?php echo $value[\'id\']; ?>\').farbtastic(\'#<?php echo $value[\'id\']; ?>\');

                });
                </script>

                <div class="textcont">
                    <div class="value">
                        <?php echo $value[\'name\']; ?>:
                    </div>
                    <div id="colourpicker-<?php echo $value[\'id\']; ?>"></div>
                    <div class="input">
                        <input name="<?php echo $value[\'id\']; ?>" id="<?php echo $value[\'id\']; ?>" type="<?php echo $value[\'type\']; ?>" value="<?php if ( get_settings( $value[\'id\'] ) != "") { echo stripslashes(htmlspecialchars(get_settings( $value[\'id\'] ), ENT_QUOTES)); } else { echo $value[\'std\']; } ?>" />
                        <p><?php echo stripslashes(htmlspecialchars($value[\'desc\'])); ?></p>
                    </div>
                    <div class="clear"></div>
                </div>


                <?php break;

                case "file": // style the upload boxes
                ?>

                <div class="textcont">
                    <div class="value">
                        <?php echo $value[\'name\']; ?>:
                    </div>
                    <div class="input">
                        <table class="form-table">
                            <tr valign="top">
                                <th scope="row">File:</th>
                                <td><input type="file" name="<?php echo $value[\'id\']; ?>" class="tz-upload" size="40" border="0" /></td>
                            </tr>
                        </table>
                            <p><?php _e(\'Current file:\', \'framework\') ?> <?php echo get_option($value[\'id\']); ?></p>
                    </div>
                    <div class="clear"></div>
                </div>


                <?php break;

                case "textarea": // style the textareas
                ?>

                <div class="textcont">
                <div class="value"><?php echo $value[\'name\']; ?>:</div>
                <div class="input">
                <textarea name="<?php echo $value[\'id\']; ?>" id="<?php echo $value[\'id\']; ?>" type="<?php echo $value[\'type\']; ?>"><?php if ( get_settings( $value[\'id\'] ) != "") { echo stripslashes(htmlspecialchars(get_settings( $value[\'id\'] ), ENT_QUOTES)); } else { echo $value[\'std\']; } ?></textarea>
                <p><?php echo stripslashes(htmlspecialchars($value[\'desc\'])); ?></p>
                </div>
                <div class="clear"></div>
                </div>


                <?php break;

                case "checkbox": // style the checkboxes
                ?>

                <div class="textcont">
                <div class="value check-value"><?php echo $value[\'name\']; ?>:</div>
                <div class="input">
                <?php if(get_option($value[\'id\'])){ $checked = "checked=\\"checked\\""; }else{ $checked = "";} ?>
                <p><input type="checkbox" name="<?php echo $value[\'id\']; ?>" id="<?php echo $value[\'id\']; ?>" value="true" <?php echo $checked; ?> />
                <?php echo stripslashes(htmlspecialchars($value[\'desc\'])); ?></p>
                </div>
                <div class="clear"></div>
                </div>


                <?php break;

                case "select": // style the select
                ?>

                <div class="textcont">
                <div class="value"><?php echo $value[\'name\']; ?>:</div>
                <div class="input">
                <select name="<?php echo $value[\'id\']; ?>" id="<?php echo $value[\'id\']; ?>"><?php foreach ($value[\'options\'] as $option) { ?><option<?php if ( get_settings( $value[\'id\'] ) == $option) { echo \' selected="selected"\'; } elseif ($option == $value[\'std\']) { echo \' selected="selected"\'; } ?>><?php echo $option; ?></option><?php } ?></select>
                <p><?php echo stripslashes(htmlspecialchars($value[\'desc\'])); ?></p>
                </div>
                <div class="clear"></div>
                </div>


                <?php break;

                case "close": // style the closing
                ?>

                    </div><!-- inside -->
                </div><!-- post box -->

                <p class="submit">
                        <input name="save" type="submit" class="button" value="Save Settings" />    
                        <input type="hidden" name="action" value="save" />
                    </p>

                <?php break;
                } 
            }
            ?>

            </div><!-- postbox container -->
            </div><!-- metabox holder -->


            </form>
            <form method="post" action="">
                <p class="submit">
                    <input name="reset" type="submit" class="button" value="Reset" />
                    <input type="hidden" name="action" value="reset" />
                    <span><?php _e(\'Caution: will restore theme defaults\', \'framework\') ?></span>
                </p>
            </form>
    </div>

    <?php
    }



add_action(\'admin_menu\', \'mytheme_add_admin\');


// ==========================//
//             END the theme options!                   //
// ==========================//

?>
所以看起来this example

我想列出所有类别并自动生成唯一ID。我只能选择下拉菜单,但我需要to list all categories; i.e.类似于

array(  "name" => __("Categories", \'framework\'),
"desc" => __("Category color #1", \'framework\'),
"id" => $shortname."_cat_color#1",
"std" => "#",
"type" => "text"
"desc" => __("Category color #2", \'framework\'),
"id" => $shortname."_cat_color#2",
"std" => "#",
"type" => "text"
"desc" => __("Category color #3", \'framework\'),
"id" => $shortname."_cat_color#3",
"std" => "#",
"type" => "text"),
有什么建议吗?

1 个回复
SO网友:Bainternet

在这种情况下,您可以获得类别列表,并为每个类别创建如下文本字段:

$categories = get_categories(\'hide_empty=0&orderby=name\');
foreach ($categories as $cat){
    $options[] = array( 
    "name" => $cat->name,  
    "desc" => "Select a color for ".$cat->name." category",  
    "id" => $shortname."_cat_".$cat->term_id,  
    "type" => "text",  
    "std" => "");  
}

结束