2016-08-23 70 views
2

我有这样的代码: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([ 
     '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类(如整数),但它不工作。

+0

为什么你不使用

回答

1

您可以使用此功能的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 
?>