列出选项中的所有类别

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

我曾使用以下代码列出所有类别:

foreach($categories as $cat) {
    print \'<label>\'.$cat->name.\'</label>\';
    print \'<input type="text" title="Add value" name="\'.$themename.\'_value_\'.$cat->slug.\'" id="\'.$themename.\'_color_\'.$cat->slug.\'" value="\'.get_option($themename.\'_value_\'.$cat->slug).\'" />\';
}
它为每个类别生成列表和唯一的输入框。

但我用数组重写了结构:

i、 e。

$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 = "Name";
    $shortname = "tz";

    $options = array (

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

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

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

    array(  "name" => __("Banner Settings", \'framework\'),
    "id" => $shortname."_banner_settings",
    "type" => "title"),

    array(  "name" => __("Show Header Advert", \'framework\'),
    "desc" => __("Check this to show a banner in the header of your site (468px x 60px)", \'framework\'),
    "id" => $shortname."_banner_header",
    "std" => "false",
    "type" => "checkbox"),

    array(  "name" => __("Banner Image URL", \'framework\'),
    "desc" => __("Enter the full image URL of your banner (468px x 60px) e.g. http://www.example.com/banner.gif", \'framework\'),
    "id" => $shortname."_banner_img_url",
    "std" => get_bloginfo(\'template_directory\')."/images/header_advert.jpg",
    "type" => "text"),
其结构与上建议的相同http://blog.themeforest.net/wordpress/create-an-options-page-for-your-wordpress-theme/

重写原始方式的解决方案是什么?有什么建议吗?

(我只有一个复选框、文本、文本区域和文件类型)

1 个回复
SO网友:fuxia

要获取可选类别的列表,请使用wp_dropdown_categories().
示例:

wp_dropdown_categories(
    array (
        \'orderby\' => \'name\',
        // ID of the already selected category:
        \'selected\' => 4,
        // If no category is selected:
        \'show_option_none\' => \'Choose one\'
    )
);
HTML输出:

<select name=\'cat\' id=\'cat\' class=\'postform\' >
    <option value=\'-1\'>Choose one</option>
    <option class="level-0" value="4" selected="selected">Animals</option>
    <option class="level-0" value="6">Hyperbolica</option>
    <option class="level-0" value="5">Nonsense</option>
    <option class="level-0" value="1">Uncategorized</option>
</select>
视觉输出:

enter image description here

结束

相关推荐

Google Maps with categories

我正在寻找可以帮助我在谷歌地图和多个类别上创建多个标记的插件。这个插件将是伟大的,但我需要更好的类别管理器。例如餐厅——素食主义者——素食主义者——酒店——五星级——四星级——我还需要能够选择要显示的类别地图。谢谢你的回答。