回答編集履歴

3

$itrに代入して使うよう変更

2017/11/02 06:42

投稿

masaya_ohashi
masaya_ohashi

スコア9206

test CHANGED
@@ -30,7 +30,9 @@
30
30
 
31
31
  ```PHP
32
32
 
33
- foreach(getCurrentCategorys(1, "achievements") as $index => $category) {
33
+ $itr = getCurrentCategorys(1, "achievements");
34
+
35
+ foreach($itr as $index => $category) {
34
36
 
35
37
  echo $category['term_id'];
36
38
 

2

使い方の追記

2017/11/02 06:42

投稿

masaya_ohashi
masaya_ohashi

スコア9206

test CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  $categorys = get_the_terms($id, $tax);
8
8
 
9
- for($categorys as $i => $category) {
9
+ foreach($categorys as $i => $category) {
10
10
 
11
11
  yield [
12
12
 
@@ -25,3 +25,21 @@
25
25
  }
26
26
 
27
27
  ```
28
+
29
+ yieldで値を返すfunctionの戻り値はイテレータになっていて、foreachなどに渡して使います。
30
+
31
+ ```PHP
32
+
33
+ foreach(getCurrentCategorys(1, "achievements") as $index => $category) {
34
+
35
+ echo $category['term_id'];
36
+
37
+ echo $category['name'];
38
+
39
+ echo $category['slug'];
40
+
41
+ echo $category['totalCount'];
42
+
43
+ }
44
+
45
+ ```

1

totalCountが取れないので修正

2017/11/02 06:41

投稿

masaya_ohashi
masaya_ohashi

スコア9206

test CHANGED
@@ -8,7 +8,17 @@
8
8
 
9
9
  for($categorys as $i => $category) {
10
10
 
11
+ yield [
12
+
13
+ 'term_id' => $category->term_id,
14
+
11
- yield (array)$category;
15
+ 'name' => $category->name,
16
+
17
+ 'slug' => $category->slug,
18
+
19
+ 'totalCount' => $i,
20
+
21
+ ];
12
22
 
13
23
  }
14
24