Javad Abbasi
Javad Abbasi

Reputation: 1

How to load category ID instead of category name?

i am using propanel theme option script for wordpress theme options.

in below code , echo category name , but i want category ID ... plz help me :

// Grabs Categories from Wordpress
$tt_categories = array();  
$tt_categories_obj = get_categories('hide_empty=0');
foreach ($tt_categories_obj as $tt_cat) {
$tt_categories[$tt_cat->cat_ID] = $tt_cat->cat_name;}
$categories_tmp = array_unshift($tt_categories, "Select a category:");

//Then you would call it like so in your theme options    

//shows a select box in theme options page
$options[] = array( "name" => __('Wordpress Category','framework_localize'),
        "desc" => __('Select a category','framework_localize'),
        "id" => "wp_category",
        "std" => "1",
        "type" => "select",
        "options" => $tt_categories);  

Upvotes: 0

Views: 316

Answers (2)

Javad Abbasi
Javad Abbasi

Reputation: 1

answered :

FULL CODE :

//in theme-setting.php


//Access the WordPress Categories via an Array
$tt_categories = array();  
$tt_categories_obj = get_categories('hide_empty=0');
foreach ($tt_categories_obj as $tt_cat) {
$tt_categories[$tt_cat->cat_ID] = $tt_cat->cat_name;}
$categories_tmp = array_unshift($tt_categories, "select cat:");

//
$options[] = array( "name" => __('Tabs #1 Category','framework_localize'),
            "desc" => __('select cat.','framework_localize'),
            "id" => $shortname."_tabs1_category",
            "std" => "1",
            "type" => "select",
            "options" => $tt_categories);

in theme :

global $wpdb;
$wpnews_tabs1_category = $wpdb->get_var("SELECT term_id FROM $wpdb->terms WHERE name='$wpnews_tabs1_category'");

Upvotes: 0

diggy
diggy

Reputation: 6828

$tt_cat->cat_ID holds the category ID.

If you want the dropdown to display the IDs rather than the names, change

$tt_categories[$tt_cat->cat_ID] = $tt_cat->cat_name;

to

$tt_categories[$tt_cat->cat_ID] = $tt_cat->cat_ID;

Note that in both cases, the category ID will be saved as the option value.

Upvotes: 1

Related Questions