2016-08-23 4 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 
     } 
    } 
} 

Я хотел бы получить категорию по идентификатору (как целое число), но он не работает.

+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 
?> 
Смежные вопросы