Php Wordpress类别API
我有以下代码:Php Wordpress类别API,php,wordpress,api,Php,Wordpress,Api,我有以下代码: public static function category($id = null) { $fields = ['id'=>'cat_ID','slug'=>'slug','title'=>'name','description'=>'description','count'=>'count']; // All categories $categories = get_categories([ 'orderb
public static function category($id = null) {
$fields = ['id'=>'cat_ID','slug'=>'slug','title'=>'name','description'=>'description','count'=>'count'];
// All categories
$categories = get_categories([
'orderby'=>'id',
'order'=>'ASC'
]);
// Get all categories
if($id === null) {
return sanitize( $categories, $fields);
// Get category by its ID or Slug
} else {
if( is_numeric($id) ) {
return sanitize( find($categories, 'cat_ID', $id), $fields); // By ID
} else {
return sanitize( find($categories, 'slug', $id), $fields);// By Slug
}
}
}
我想按id获取类别(如整数),但它不起作用。您可以使用wordpress已有的这个函数
<?php
$cats = get_category(1/*change the number with your cats id*/);
echo $cats->term_id; //get the cats id
echo $cats->name; //get the cats title
echo $cats->slug; //get the cats slug
echo $cats->description; //get the cats description
echo $cats->category_count; //get the cats count
?>
为什么不使用此选项