回答編集履歴

2

2

2021/07/02 21:29

投稿

takasima20
takasima20

スコア7460

test CHANGED
@@ -35,3 +35,43 @@
35
35
  [url_box store="A"]
36
36
 
37
37
  ```
38
+
39
+ --- 追記 ---
40
+
41
+ あー、まとめて返すならこんなかんじかな。
42
+
43
+ ```php
44
+
45
+ function urlbox_shortcode(){
46
+
47
+ $urlStoreA = get_post_meta(get_the_ID(), 'urlA', true);//Aストア
48
+
49
+ $urlStoreB = get_post_meta(get_the_ID(), 'urlB', true);//Bストア
50
+
51
+ $urlStoreC = get_post_meta(get_the_ID(), 'urlC', true);//Cストア
52
+
53
+ $s = '';
54
+
55
+ if (!empty($urlStoreA)){//もし空でなければ
56
+
57
+ $s .= '<a href="' . $urlStores . '">A で購入する</a>';
58
+
59
+ }
60
+
61
+ if (!empty($urlStoreB)){//もし空でなければ
62
+
63
+ $s .= '<a href="' . $urlAmazon . '">B で購入する</a>';
64
+
65
+ }
66
+
67
+ if (!empty($urlStoreC)){//もし空でなければ
68
+
69
+ $s .= '<a href="' . $urlStoreC . '">C で購入する</a>';
70
+
71
+ }
72
+
73
+ return $s;
74
+
75
+ }
76
+
77
+ ```

1

1

2021/07/02 21:28

投稿

takasima20
takasima20

スコア7460

test CHANGED
@@ -3,3 +3,35 @@
3
3
 
4
4
 
5
5
  現在はどんなかんじのコードになっているのでしょうか。
6
+
7
+ --- 追記 ---
8
+
9
+ 個別に使うならこんなかんじでしょか。
10
+
11
+ ```php
12
+
13
+ function urlbox_shortcode($atts){
14
+
15
+ extract(shortcode_atts(array('store' => 'A'), $atts));
16
+
17
+ $stores = ['A'=>'urlA','B'=>'urlB','C'=>'urlC'];
18
+
19
+ $urlStore = get_post_meta(get_the_ID(), $stores[$store], true);
20
+
21
+ if (!empty($urlStore)){//もし空でなければ
22
+
23
+ return '<a href="' . $urlStore . '">' . $store . ' で購入する</a>';
24
+
25
+ }
26
+
27
+ }
28
+
29
+ add_shortcode( 'url_box', 'urlbox_shortcode' );
30
+
31
+ ```
32
+
33
+ ```
34
+
35
+ [url_box store="A"]
36
+
37
+ ```