回答編集履歴

5

修正

2019/01/30 09:01

投稿

papinianus
papinianus

スコア12705

test CHANGED
@@ -43,3 +43,39 @@
43
43
  ?></p>
44
44
 
45
45
  ```
46
+
47
+
48
+
49
+ 質問者様による改訂案を将来の訪問者のために共有します
50
+
51
+ ```php
52
+
53
+ <?php
54
+
55
+ $count = 2;
56
+
57
+ $tagchk = get_the_tags();
58
+
59
+ if($tagchk){ //get_the_tagsはタグがない場合、空配列ではなくfalseを返す
60
+
61
+ $wptags = array_slice(get_the_tags(), 0, $count);
62
+
63
+ echo '<p class="tag">';
64
+
65
+ foreach($wptags as $tag) {
66
+
67
+ $tagname = $tag->name;
68
+
69
+ $tagurl = get_tag_link( $tag->term_id );
70
+
71
+ echo '<a href="'.$tagurl.'">'.$tagname.'</a>';
72
+
73
+ }
74
+
75
+ echo '</p>';
76
+
77
+ }
78
+
79
+ ?>
80
+
81
+ ```

4

修正

2019/01/30 09:01

投稿

papinianus
papinianus

スコア12705

test CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  $first2 = array_slice(get_the_tags(), 0, $count);
12
12
 
13
- $first_2 = array_slice(explode(":", the_tags("", ":", "")), 0, $count); //:はタグで使わない文字である必要がある
13
+ //→出力されちゃう// $first_2 = array_slice(explode(":", the_tags("", ":", "")), 0, $count); //:はタグで使わない文字である必要がある
14
14
 
15
15
  ```
16
16
 
@@ -32,7 +32,9 @@
32
32
 
33
33
  foreach($wptags as $tag) {
34
34
 
35
+ $stringify .= '<a href="'.get_tag_link( $tag->term_id ).'">'.$tag->name.'</a> ' ;
36
+
35
- $stringify .= $tag->name;
37
+ //$stringify .= $tag->name;
36
38
 
37
39
  }
38
40
 

3

修正

2019/01/30 08:01

投稿

papinianus
papinianus

スコア12705

test CHANGED
@@ -26,9 +26,17 @@
26
26
 
27
27
 
28
28
 
29
- $first2 = array_slice(get_the_tags(), 0, $count);
29
+ $wptags = array_slice(get_the_tags(), 0, $count);
30
30
 
31
- echo implode("",$first2);
31
+ $stringify = "";
32
+
33
+ foreach($wptags as $tag) {
34
+
35
+ $stringify .= $tag->name;
36
+
37
+ }
38
+
39
+ echo $stringify;
32
40
 
33
41
  ?></p>
34
42
 

2

修正

2019/01/30 07:45

投稿

papinianus
papinianus

スコア12705

test CHANGED
@@ -8,9 +8,9 @@
8
8
 
9
9
 
10
10
 
11
- $first2 = array_slice(get_the_tag(), 0, $count);
11
+ $first2 = array_slice(get_the_tags(), 0, $count);
12
12
 
13
- $first_2 = array_slice(explode(":", the_tag("", ":", "")), 0, $count); //:はタグで使わない文字である必要がある
13
+ $first_2 = array_slice(explode(":", the_tags("", ":", "")), 0, $count); //:はタグで使わない文字である必要がある
14
14
 
15
15
  ```
16
16
 
@@ -26,9 +26,9 @@
26
26
 
27
27
 
28
28
 
29
- $first2 = array_slice(get_the_tag(), 0, $count);
29
+ $first2 = array_slice(get_the_tags(), 0, $count);
30
30
 
31
- echo $first2;
31
+ echo implode("",$first2);
32
32
 
33
33
  ?></p>
34
34
 

1

修正

2019/01/30 07:28

投稿

papinianus
papinianus

スコア12705

test CHANGED
@@ -13,3 +13,23 @@
13
13
  $first_2 = array_slice(explode(":", the_tag("", ":", "")), 0, $count); //:はタグで使わない文字である必要がある
14
14
 
15
15
  ```
16
+
17
+ --
18
+
19
+ the_tagは出力関数でした。申し訳ないです。↓でどうでしょうか?
20
+
21
+ ```php
22
+
23
+ <p class="tag"><?php
24
+
25
+ $count = 2; //2個?
26
+
27
+
28
+
29
+ $first2 = array_slice(get_the_tag(), 0, $count);
30
+
31
+ echo $first2;
32
+
33
+ ?></p>
34
+
35
+ ```