List all categories in options

I used to use the following code to make the list of all categories:

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).'" />';
}

It generates the list and unique input box for each category.

Read More

But I have rewritten the structure using arrays:

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"),

It is the same structure as suggested on http://blog.themeforest.net/wordpress/create-an-options-page-for-your-wordpress-theme/

What would be the solution to rewrite the original way? Any suggestions?

(I have just a checkbox, text, textarea and file types)

Related posts

Leave a Reply

1 comment

  1. To get a list of selectable categories use wp_dropdown_categories().
    Example:

    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 output:

    <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>
    

    Visual output:

    enter image description here