post_api.php
2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
require_once( dirname(__FILE__) . '/../wp-load.php' );
global $wpdb;
ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL);
$type = isset($_REQUEST['w'])?$_REQUEST['w']:'';
switch($type){
case 'products_category': //产品分类
$categories = get_hierarchical_categories('category');
echo json_encode(['status'=>200,'data'=>$categories]);
break;
case 'news_category'://新闻分类
$categories = get_hierarchical_categories('news_catalog');
echo json_encode(['status'=>200,'data'=>$categories,'message'=>'sucess']);
break;
case 'add_products_category': //产品分类新增
$name = isset($_REQUEST['name'])?trim($_REQUEST['name']):'';
$parent = isset($_REQUEST['parent'])?$_REQUEST['parent']:0;
$category_id = wp_create_category($name,$parent);
print_r($category_id);exit;
if ($category_id && !is_wp_error($category_id)) {
echo json_encode(['status'=>200,'data'=>'','message'=>'sucess']);
} else {
echo json_encode(['status'=>1,'data'=>'','message'=>'failed']);
}
break;
case 'add_news_category'://新闻分类新增
break;
case 'add_product'://产品新增
break;
case 'add_news'://新闻新增
break;
default:
echo 2;exit;
}
//获取有层级的分类
function get_hierarchical_categories($taxonomy='category',$parent_id = 0) {
$args = array(
'taxonomy' => $taxonomy,
'parent' => $parent_id,
'hide_empty' => false,
);
$categories = get_categories($args);
if (empty($categories)) {
return array();
}
$result = array();
foreach ($categories as $category) {
$category_data = array(
'id' => $category->term_id,
'name' => $category->name,
'slug' => $category->slug,
//'count' => $category->count,
'parent' => $category->parent,
'children' => get_hierarchical_categories($taxonomy,$category->term_id)
);
if(!in_array($category->slug,['featured','featured-products'])){
$result[] = $category_data;
}
}
return $result;
}