[英]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 : 在下面的代码中,回显类别名称,但是我想要类别ID ... plz可以帮助我:
// 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);
$tt_cat->cat_ID
holds the category ID. $tt_cat->cat_ID
保存类别ID。
If you want the dropdown to display the IDs rather than the names, change 如果要下拉列表显示ID而不是名称,请更改
$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. 请注意,在两种情况下,类别ID都将保存为选项值。
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'");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.