$cats= get_categories(array(
'order' => 'ASC',
'orderby' => 'id',
'hierarchical' => 0,
'hide_empty' => 0,
'taxonomy' => 'edu_year'
));
我想生成一個變量,其中包含從get_categories函數檢索到的值的ID的數組。在wordpress中獲取一個類別id的數組?
$cats= get_categories(array(
'order' => 'ASC',
'orderby' => 'id',
'hierarchical' => 0,
'hide_empty' => 0,
'taxonomy' => 'edu_year'
));
我想生成一個變量,其中包含從get_categories函數檢索到的值的ID的數組。在wordpress中獲取一個類別id的數組?
根據相關文檔返回:
$category->term_id
$category->name
$category->slug
$category->term_group
$category->term_taxonomy_id
$category->taxonomy
$category->description
$category->parent
$category->count
$category->cat_ID
$category->category_count
$category->category_description
$category->cat_name
$category->category_nicename
$category->category_parent
所以:
$output_categories = array();
$categories=get_categories($args);
foreach($categories as $category) {
$output_categories[$category->cat_ID] = $category->name;
}
然後$output_categories
是你所需要的東西。
當使用get_terms:
<?php
$categories_ids = get_terms(
array('category'), // Taxonomies
array('fields' => 'ids') // Fields
);
?>
我發現get_terms('category'array('fields'=>'id => slug'));甚至更有用。 – 2014-10-10 22:55:49
我想有一個包含$變量=陣列(3,4,5),其中3,4,5是類別的ID檢索到的 – 2012-03-20 16:12:44
所以可變那麼只要做$ output_categories [] = $ category-> cat_ID; – wesside 2012-03-20 16:53:03