如何加载类别ID而不是类别名称?

时间:2014-12-17 18:48:32

标签: php wordpress themes option setting

我正在使用propanel theme option脚本进行wordpress主题选项。

在下面的代码中,echo类别名称,但我想类别ID ... 请帮助我:

// 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);  

2 个答案:

答案 0 :(得分:1)

$tt_cat->cat_ID包含类别ID。

如果您希望下拉列表显示ID而不是名称,请更改

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

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

请注意,在这两种情况下,类别ID都将保存为选项值。

答案 1 :(得分:0)

回答:

完整代码:

//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);
主题中的

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