質問編集履歴

9

example

2018/10/14 07:04

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -96,11 +96,11 @@
96
96
 
97
97
 
98
98
 
99
- $html .= '<div class="hamachonews">';
99
+ $html .= '<div class="news">';
100
100
 
101
101
  $html .= '<a href="http://example.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html" class="bwWrapper"><img src="'.$custom_fields['display_image'][0].'" alt="'.$custom_fields['display_title'][0].'"></a>';
102
102
 
103
- $html .= '<div class="txtarea"><a href="http://hamacho.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html">';
103
+ $html .= '<div class="txtarea"><a href="http://example.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html">';
104
104
 
105
105
  $html .= '<ul class="clearfix">';
106
106
 

8

function.php追加

2018/10/14 07:04

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -213,3 +213,79 @@
213
213
  </script>
214
214
 
215
215
  ```
216
+
217
+
218
+
219
+ function.php
220
+
221
+ ```ここに言語を入力
222
+
223
+ add_action('admin_menu', 'add_custom_fields');
224
+
225
+ add_action('save_post', 'save_custom_fields');
226
+
227
+ function add_custom_fields() {
228
+
229
+ add_meta_box( 'my_sectionid', '表示用追加項目', 'my_custom_fields', 'post');
230
+
231
+ }
232
+
233
+ function my_custom_fields() {
234
+
235
+ global $post;
236
+
237
+ $display_image = get_post_meta($post->ID,'display_image',true);
238
+
239
+ $display_number = get_post_meta($post->ID,'display_number',true);
240
+
241
+ $display_title = get_post_meta($post->ID,'display_title',true);
242
+
243
+ $display_text = get_post_meta($post->ID,'display_text',true);
244
+
245
+ echo '<p>表示するサムネイルの画像パス<br>';
246
+
247
+ echo '<input type="text" name="display_image" value="'.esc_html($display_image).'" size="60" /></p>';
248
+
249
+ echo '<p>表示するナンバー<br>';
250
+
251
+ echo '<input type="text" name="display_number" value="'.esc_html($display_number).'" size="60" /></p>';
252
+
253
+ echo '<p>表示するタイトル<br>';
254
+
255
+ echo '<input type="text" name="display_title" value="'.esc_html($display_title).'" size="60" /></p>';
256
+
257
+ echo '<p>表示するテキスト<br>';
258
+
259
+ echo '<input type="text" name="display_text" value="'.esc_html($display_text).'" size="60" /></p>';
260
+
261
+ }
262
+
263
+ function save_custom_fields( $post_id ) {
264
+
265
+ if(!empty($_POST['display_image']))
266
+
267
+ update_post_meta($post_id, 'display_image', $_POST['display_image'] );
268
+
269
+ else delete_post_meta($post_id, 'display_image');
270
+
271
+ if(!empty($_POST['display_number']))
272
+
273
+ update_post_meta($post_id, 'display_number', $_POST['display_number'] );
274
+
275
+ else delete_post_meta($post_id, 'display_number');
276
+
277
+ if(!empty($_POST['display_title']))
278
+
279
+ update_post_meta($post_id, 'display_title', $_POST['display_title'] );
280
+
281
+ else delete_post_meta($post_id, 'display_title');
282
+
283
+ if(!empty($_POST['display_text']))
284
+
285
+ update_post_meta($post_id, 'display_text', $_POST['display_text'] );
286
+
287
+ else delete_post_meta($post_id, 'display_text');
288
+
289
+ }
290
+
291
+ ```

7

example

2018/10/14 06:58

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -98,7 +98,7 @@
98
98
 
99
99
  $html .= '<div class="hamachonews">';
100
100
 
101
- $html .= '<a href="http://hamacho.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html" class="bwWrapper"><img src="'.$custom_fields['display_image'][0].'" alt="'.$custom_fields['display_title'][0].'"></a>';
101
+ $html .= '<a href="http://example.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html" class="bwWrapper"><img src="'.$custom_fields['display_image'][0].'" alt="'.$custom_fields['display_title'][0].'"></a>';
102
102
 
103
103
  $html .= '<div class="txtarea"><a href="http://hamacho.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html">';
104
104
 
@@ -112,9 +112,9 @@
112
112
 
113
113
  $html .= '<h3>'.$custom_fields['display_title'][0].'</h3>';
114
114
 
115
- // $html .= '<h3><a href="http://hamacho.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html">'.$custom_fields['display_title'][0].'</a></h3>';
115
+ // $html .= '<h3><a href="http://example.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html">'.$custom_fields['display_title'][0].'</a></h3>';
116
-
116
+
117
- // $html .= '<a href="http://hamacho.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html">'.$custom_fields['display_text'][0].'</a>';
117
+ // $html .= '<a href="http://example.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html">'.$custom_fields['display_text'][0].'</a>';
118
118
 
119
119
  $html .= '</a></div>';
120
120
 

6

phpをJSで吐き出して表示させる

2018/10/14 06:57

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -32,8 +32,58 @@
32
32
 
33
33
  なお具体的なコードは以下になります。
34
34
 
35
+
36
+
37
+
38
+
39
+ more-disp.php
40
+
35
41
  ```
36
42
 
43
+
44
+
45
+ <?php
46
+
47
+
48
+
49
+ require_once("../../../wp-load.php");
50
+
51
+ require_once("../../../wp-config.php");
52
+
53
+ $now_post_num = $_POST['now_post_num'];
54
+
55
+ $get_post_num = $_POST['get_post_num'];
56
+
57
+ $next_now_post_num = $now_post_num + $get_post_num;
58
+
59
+ $next_get_post_num = $get_post_num + $get_post_num;
60
+
61
+
62
+
63
+ $sql = "SELECT $wpdb->posts.ID FROM $wpdb->posts WHERE $wpdb->posts.post_type = 'post' AND $wpdb->posts.post_status = 'publish' ORDER BY $wpdb->posts.post_date DESC LIMIT %d, %d";
64
+
65
+ $pre = $wpdb->prepare($sql,$now_post_num,$get_post_num);
66
+
67
+ $results = $wpdb->get_results($pre);
68
+
69
+
70
+
71
+ $next_pre = $wpdb->prepare($sql,$next_now_post_num,$next_get_post_num);
72
+
73
+ $next_results = $wpdb->get_results($next_pre);
74
+
75
+
76
+
77
+ $noDataFlg = 0;
78
+
79
+ if ( count($results) < $get_post_num || !count($next_results) ) {
80
+
81
+ $noDataFlg = 1;
82
+
83
+ }
84
+
85
+
86
+
37
87
  $html = "";
38
88
 
39
89
 
@@ -46,26 +96,120 @@
46
96
 
47
97
 
48
98
 
49
- $html .= '<div class="news">';
50
-
51
- $html .= '<a href="http://exmaple.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html" class="bwWrapper"><img src="'.$custom_fields['display_image'][0].'" alt="'.$custom_fields['display_title'][0].'"></a>';
52
-
53
- $html .= '<div class="txtarea"><a href="http://exmaple.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html">';
99
+ $html .= '<div class="hamachonews">';
100
+
101
+ $html .= '<a href="http://hamacho.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html" class="bwWrapper"><img src="'.$custom_fields['display_image'][0].'" alt="'.$custom_fields['display_title'][0].'"></a>';
102
+
103
+ $html .= '<div class="txtarea"><a href="http://hamacho.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html">';
54
104
 
55
105
  $html .= '<ul class="clearfix">';
56
106
 
57
107
  $html .= '<li>'.$custom_fields['display_number'][0].'</li>';
58
108
 
59
- **$html .= '<li>'.get_the_time('Y/m/d').'</li>';**
109
+ $html .= '<li>'.get_the_time('Y/m/d').'</li>';
60
110
 
61
111
  $html .= '</ul>';
62
112
 
63
113
  $html .= '<h3>'.$custom_fields['display_title'][0].'</h3>';
64
114
 
115
+ // $html .= '<h3><a href="http://hamacho.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html">'.$custom_fields['display_title'][0].'</a></h3>';
116
+
117
+ // $html .= '<a href="http://hamacho.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html">'.$custom_fields['display_text'][0].'</a>';
118
+
65
119
  $html .= '</a></div>';
66
120
 
67
121
  $html .= '</div>';
68
122
 
69
123
  }
70
124
 
125
+
126
+
127
+ $returnObj = array();
128
+
129
+ $returnObj = array(
130
+
131
+ 'noDataFlg' => $noDataFlg,
132
+
133
+ 'html' => $html,
134
+
135
+ );
136
+
137
+ $returnObj = json_encode($returnObj);
138
+
139
+
140
+
141
+ echo $returnObj;
142
+
143
+
144
+
145
+ ?>
146
+
147
+
148
+
71
149
  ```
150
+
151
+
152
+
153
+ JS
154
+
155
+ ```ここに言語を入力
156
+
157
+
158
+
159
+
160
+
161
+ <script type="text/javascript">
162
+
163
+ var now_post_num = 6; // 現在表示されている数
164
+
165
+ var get_post_num = 6; // 一度に取得する数
166
+
167
+
168
+
169
+ $("a#more_disp").on("click", function() {
170
+
171
+ $.ajax({
172
+
173
+ type: 'post',
174
+
175
+ url: '<?php bloginfo('template_url'); ?>/more-disp.php',
176
+
177
+ data: {
178
+
179
+ 'now_post_num': now_post_num,
180
+
181
+ 'get_post_num': get_post_num
182
+
183
+ },
184
+
185
+ success: function(data) {
186
+
187
+ now_post_num = now_post_num + get_post_num;
188
+
189
+ console.log(data);
190
+
191
+ data = JSON.parse(data);
192
+
193
+ console.log(data);
194
+
195
+ $(".post-list").append(data['html']);
196
+
197
+ $('.bwWrapper').BlackAndWhite();
198
+
199
+ if (data['noDataFlg']) {
200
+
201
+ $("#more_disp").remove();
202
+
203
+ }
204
+
205
+ }
206
+
207
+ });
208
+
209
+ return false;
210
+
211
+ });
212
+
213
+ </script>
214
+
215
+ ```

5

URLをexmaple変更 / codeタグで囲む

2018/10/14 06:56

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -32,7 +32,7 @@
32
32
 
33
33
  なお具体的なコードは以下になります。
34
34
 
35
-
35
+ ```
36
36
 
37
37
  $html = "";
38
38
 
@@ -48,9 +48,9 @@
48
48
 
49
49
  $html .= '<div class="news">';
50
50
 
51
- $html .= '<a href="http://xxx.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html" class="bwWrapper"><img src="'.$custom_fields['display_image'][0].'" alt="'.$custom_fields['display_title'][0].'"></a>';
51
+ $html .= '<a href="http://exmaple.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html" class="bwWrapper"><img src="'.$custom_fields['display_image'][0].'" alt="'.$custom_fields['display_title'][0].'"></a>';
52
52
 
53
- $html .= '<div class="txtarea"><a href="http://xxx.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html">';
53
+ $html .= '<div class="txtarea"><a href="http://exmaple.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html">';
54
54
 
55
55
  $html .= '<ul class="clearfix">';
56
56
 
@@ -67,3 +67,5 @@
67
67
  $html .= '</div>';
68
68
 
69
69
  }
70
+
71
+ ```

4

該当箇所を太字に変更

2018/10/14 06:52

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -56,7 +56,7 @@
56
56
 
57
57
  $html .= '<li>'.$custom_fields['display_number'][0].'</li>';
58
58
 
59
- $html .= '<li>'.get_the_time('Y/m/d').'</li>';
59
+ **$html .= '<li>'.get_the_time('Y/m/d').'</li>';**
60
60
 
61
61
  $html .= '</ul>';
62
62
 

3

URLをxxxに変更

2018/10/14 06:38

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -50,7 +50,7 @@
50
50
 
51
51
  $html .= '<a href="http://xxx.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html" class="bwWrapper"><img src="'.$custom_fields['display_image'][0].'" alt="'.$custom_fields['display_title'][0].'"></a>';
52
52
 
53
- $html .= '<div class="txtarea"><a href="http://hamacho.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html">';
53
+ $html .= '<div class="txtarea"><a href="http://xxx.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html">';
54
54
 
55
55
  $html .= '<ul class="clearfix">';
56
56
 

2

URLをxxxに変更

2018/10/14 06:37

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -48,7 +48,7 @@
48
48
 
49
49
  $html .= '<div class="news">';
50
50
 
51
- $html .= '<a href="http://hamacho.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html" class="bwWrapper"><img src="'.$custom_fields['display_image'][0].'" alt="'.$custom_fields['display_title'][0].'"></a>';
51
+ $html .= '<a href="http://xxx.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html" class="bwWrapper"><img src="'.$custom_fields['display_image'][0].'" alt="'.$custom_fields['display_title'][0].'"></a>';
52
52
 
53
53
  $html .= '<div class="txtarea"><a href="http://hamacho.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html">';
54
54
 

1

具体的なソースコード

2018/10/14 06:37

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -25,3 +25,45 @@
25
25
 
26
26
 
27
27
  何卒よろしくお願い致します。
28
+
29
+
30
+
31
+
32
+
33
+ なお具体的なコードは以下になります。
34
+
35
+
36
+
37
+ $html = "";
38
+
39
+
40
+
41
+ foreach ($results as $result) {
42
+
43
+ $custom_fields = get_post_custom($result->ID);
44
+
45
+ $categories = get_the_terms($result->ID, "category");
46
+
47
+
48
+
49
+ $html .= '<div class="news">';
50
+
51
+ $html .= '<a href="http://hamacho.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html" class="bwWrapper"><img src="'.$custom_fields['display_image'][0].'" alt="'.$custom_fields['display_title'][0].'"></a>';
52
+
53
+ $html .= '<div class="txtarea"><a href="http://hamacho.jp/'.$categories[0]->slug.'/p='.$result->ID.'.html">';
54
+
55
+ $html .= '<ul class="clearfix">';
56
+
57
+ $html .= '<li>'.$custom_fields['display_number'][0].'</li>';
58
+
59
+ $html .= '<li>'.get_the_time('Y/m/d').'</li>';
60
+
61
+ $html .= '</ul>';
62
+
63
+ $html .= '<h3>'.$custom_fields['display_title'][0].'</h3>';
64
+
65
+ $html .= '</a></div>';
66
+
67
+ $html .= '</div>';
68
+
69
+ }