質問編集履歴
1
使用コード・現状の出力結果の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -33,3 +33,439 @@
|
|
33
33
|
|
34
34
|
|
35
35
|
宜しくお願い致します。
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
/*追記 180319
|
40
|
+
|
41
|
+
ご質問いただきました実際に使用しているコードは以下の通りです。
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
```php
|
46
|
+
|
47
|
+
function get_cpt_calendar($cpt, $initial = true, $echo = true) {
|
48
|
+
|
49
|
+
global $wpdb, $m, $monthnum, $year, $wp_locale, $posts;
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
$cache = array();
|
54
|
+
|
55
|
+
$key = md5( $m . $monthnum . $year );
|
56
|
+
|
57
|
+
if ( $cache = wp_cache_get( 'get_calendar', 'calendar' ) ) {
|
58
|
+
|
59
|
+
if ( is_array($cache) && isset( $cache[ $key ] ) ) {
|
60
|
+
|
61
|
+
if ( $echo ) {
|
62
|
+
|
63
|
+
echo apply_filters( 'get_calendar', $cache[$key] );
|
64
|
+
|
65
|
+
return;
|
66
|
+
|
67
|
+
} else {
|
68
|
+
|
69
|
+
return apply_filters( 'get_calendar', $cache[$key] );
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
}
|
74
|
+
|
75
|
+
}
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
if ( !is_array($cache) )
|
80
|
+
|
81
|
+
$cache = array();
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
// Quick check. If we have no posts at all, abort!
|
86
|
+
|
87
|
+
if ( !$posts ) {
|
88
|
+
|
89
|
+
$gotsome = $wpdb->get_var("SELECT 1 as test FROM $wpdb->posts WHERE post_type = '$cpt' AND post_status = 'publish' LIMIT 1");
|
90
|
+
|
91
|
+
if ( !$gotsome ) {
|
92
|
+
|
93
|
+
$cache[ $key ] = '';
|
94
|
+
|
95
|
+
wp_cache_set( 'get_calendar', $cache, 'calendar' );
|
96
|
+
|
97
|
+
return;
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
if ( isset($_GET['w']) )
|
106
|
+
|
107
|
+
$w = ''.intval($_GET['w']);
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
// week_begins = 0 stands for Sunday
|
112
|
+
|
113
|
+
$week_begins = intval(get_option('start_of_week'));
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
// Let's figure out when we are
|
118
|
+
|
119
|
+
if ( !empty($monthnum) && !empty($year) ) {
|
120
|
+
|
121
|
+
$thismonth = ''.zeroise(intval($monthnum), 2);
|
122
|
+
|
123
|
+
$thisyear = ''.intval($year);
|
124
|
+
|
125
|
+
} elseif ( !empty($w) ) {
|
126
|
+
|
127
|
+
// We need to get the month from MySQL
|
128
|
+
|
129
|
+
$thisyear = ''.intval(substr($m, 0, 4));
|
130
|
+
|
131
|
+
$d = (($w - 1) * 7) + 6; //it seems MySQL's weeks disagree with PHP's
|
132
|
+
|
133
|
+
$thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL $d DAY) ), '%m')");
|
134
|
+
|
135
|
+
} elseif ( !empty($m) ) {
|
136
|
+
|
137
|
+
$thisyear = ''.intval(substr($m, 0, 4));
|
138
|
+
|
139
|
+
if ( strlen($m) < 6 )
|
140
|
+
|
141
|
+
$thismonth = '01';
|
142
|
+
|
143
|
+
else
|
144
|
+
|
145
|
+
$thismonth = ''.zeroise(intval(substr($m, 4, 2)), 2);
|
146
|
+
|
147
|
+
} else {
|
148
|
+
|
149
|
+
$thisyear = gmdate('Y', current_time('timestamp'));
|
150
|
+
|
151
|
+
$thismonth = gmdate('m', current_time('timestamp'));
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
$unixmonth = mktime(0, 0 , 0, $thismonth, 1, $thisyear);
|
158
|
+
|
159
|
+
$last_day = date('t', $unixmonth);
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
// Get the next and previous month and year with at least one post
|
164
|
+
|
165
|
+
$previous = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
|
166
|
+
|
167
|
+
FROM $wpdb->posts
|
168
|
+
|
169
|
+
WHERE post_date < '$thisyear-$thismonth-01'
|
170
|
+
|
171
|
+
AND post_type = '$cpt' AND post_status = 'publish'
|
172
|
+
|
173
|
+
ORDER BY post_date DESC
|
174
|
+
|
175
|
+
LIMIT 1");
|
176
|
+
|
177
|
+
$next = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
|
178
|
+
|
179
|
+
FROM $wpdb->posts
|
180
|
+
|
181
|
+
WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59'
|
182
|
+
|
183
|
+
AND post_type = '$cpt' AND post_status = 'publish'
|
184
|
+
|
185
|
+
ORDER BY post_date ASC
|
186
|
+
|
187
|
+
LIMIT 1");
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
/* translators: Calendar caption: 1: month name, 2: 4-digit year */
|
192
|
+
|
193
|
+
$calendar_caption = _x('%1$s %2$s', 'calendar caption');
|
194
|
+
|
195
|
+
$calendar_output = '<table id="wp-calendar">
|
196
|
+
|
197
|
+
<caption>' . sprintf($calendar_caption, $wp_locale->get_month($thismonth), date('Y', $unixmonth)) . '</caption>
|
198
|
+
|
199
|
+
<thead>
|
200
|
+
|
201
|
+
<tr>';
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
$myweek = array();
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
for ( $wdcount=0; $wdcount<=6; $wdcount++ ) {
|
210
|
+
|
211
|
+
$myweek[] = $wp_locale->get_weekday(($wdcount+$week_begins)%7);
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
foreach ( $myweek as $wd ) {
|
218
|
+
|
219
|
+
$day_name = (true == $initial) ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd);
|
220
|
+
|
221
|
+
$wd = esc_attr($wd);
|
222
|
+
|
223
|
+
$calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>";
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
$calendar_output .= '
|
230
|
+
|
231
|
+
</tr>
|
232
|
+
|
233
|
+
</thead>
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
<tfoot>
|
238
|
+
|
239
|
+
<tr>';
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
if ( $previous ) {
|
244
|
+
|
245
|
+
$calendar_output .= "\n\t\t".'<td colspan="3" id="prev"><a href="' . get_month_link($previous->year, $previous->month) . '?post_type='.$cpt.'" title="' . esc_attr( sprintf(__('View posts for %1$s %2$s'), $wp_locale->get_month($previous->month), date('Y', mktime(0, 0 , 0, $previous->month, 1, $previous->year)))) . '">« ' . $wp_locale->get_month_abbrev($wp_locale->get_month($previous->month)) . '</a></td>';
|
246
|
+
|
247
|
+
} else {
|
248
|
+
|
249
|
+
$calendar_output .= "\n\t\t".'<td colspan="3" id="prev" class="pad"> </td>';
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
$calendar_output .= "\n\t\t".'<td class="pad"> </td>';
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
if ( $next ) {
|
260
|
+
|
261
|
+
$calendar_output .= "\n\t\t".'<td colspan="3" id="next"><a href="' . get_month_link($next->year, $next->month) . '?post_type='.$cpt.'" title="' . esc_attr( sprintf(__('View posts for %1$s %2$s'), $wp_locale->get_month($next->month), date('Y', mktime(0, 0 , 0, $next->month, 1, $next->year))) ) . '">' . $wp_locale->get_month_abbrev($wp_locale->get_month($next->month)) . ' »</a></td>';
|
262
|
+
|
263
|
+
} else {
|
264
|
+
|
265
|
+
$calendar_output .= "\n\t\t".'<td colspan="3" id="next" class="pad"> </td>';
|
266
|
+
|
267
|
+
}
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
$calendar_output .= '
|
272
|
+
|
273
|
+
</tr>
|
274
|
+
|
275
|
+
</tfoot>
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
<tbody>
|
280
|
+
|
281
|
+
<tr>';
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
// Get days with posts
|
286
|
+
|
287
|
+
$dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date)
|
288
|
+
|
289
|
+
FROM $wpdb->posts WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00'
|
290
|
+
|
291
|
+
AND post_type = '$cpt' AND post_status = 'publish'
|
292
|
+
|
293
|
+
AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", ARRAY_N);
|
294
|
+
|
295
|
+
if ( $dayswithposts ) {
|
296
|
+
|
297
|
+
foreach ( (array) $dayswithposts as $daywith ) {
|
298
|
+
|
299
|
+
$daywithpost[] = $daywith[0];
|
300
|
+
|
301
|
+
}
|
302
|
+
|
303
|
+
} else {
|
304
|
+
|
305
|
+
$daywithpost = array();
|
306
|
+
|
307
|
+
}
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'camino') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'safari') !== false)
|
312
|
+
|
313
|
+
$ak_title_separator = "\n";
|
314
|
+
|
315
|
+
else
|
316
|
+
|
317
|
+
$ak_title_separator = ', ';
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
$ak_titles_for_day = array();
|
322
|
+
|
323
|
+
$ak_post_titles = $wpdb->get_results("SELECT ID, post_title, DAYOFMONTH(post_date) as dom "
|
324
|
+
|
325
|
+
."FROM $wpdb->posts "
|
326
|
+
|
327
|
+
."WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00' "
|
328
|
+
|
329
|
+
."AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59' "
|
330
|
+
|
331
|
+
."AND post_type = '$cpt' AND post_status = 'publish'"
|
332
|
+
|
333
|
+
);
|
334
|
+
|
335
|
+
if ( $ak_post_titles ) {
|
336
|
+
|
337
|
+
foreach ( (array) $ak_post_titles as $ak_post_title ) {
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
/** This filter is documented in wp-includes/post-template.php */
|
342
|
+
|
343
|
+
$post_title = esc_attr( apply_filters( 'the_title', $ak_post_title->post_title, $ak_post_title->ID ) );
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
if ( empty($ak_titles_for_day['day_'.$ak_post_title->dom]) )
|
348
|
+
|
349
|
+
$ak_titles_for_day['day_'.$ak_post_title->dom] = '';
|
350
|
+
|
351
|
+
if ( empty($ak_titles_for_day["$ak_post_title->dom"]) ) // first one
|
352
|
+
|
353
|
+
$ak_titles_for_day["$ak_post_title->dom"] = $post_title;
|
354
|
+
|
355
|
+
else
|
356
|
+
|
357
|
+
$ak_titles_for_day["$ak_post_title->dom"] .= $ak_title_separator . $post_title;
|
358
|
+
|
359
|
+
}
|
360
|
+
|
361
|
+
}
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
// See how much we should pad in the beginning
|
366
|
+
|
367
|
+
$pad = calendar_week_mod(date('w', $unixmonth)-$week_begins);
|
368
|
+
|
369
|
+
if ( 0 != $pad )
|
370
|
+
|
371
|
+
$calendar_output .= "\n\t\t".'<td colspan="'. esc_attr($pad) .'" class="pad"> </td>';
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
$daysinmonth = intval(date('t', $unixmonth));
|
376
|
+
|
377
|
+
for ( $day = 1; $day <= $daysinmonth; ++$day ) {
|
378
|
+
|
379
|
+
if ( isset($newrow) && $newrow )
|
380
|
+
|
381
|
+
$calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t";
|
382
|
+
|
383
|
+
$newrow = false;
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
if ( $day == gmdate('j', current_time('timestamp')) && $thismonth == gmdate('m', current_time('timestamp')) && $thisyear == gmdate('Y', current_time('timestamp')) )
|
388
|
+
|
389
|
+
$calendar_output .= '<td id="today">';
|
390
|
+
|
391
|
+
else
|
392
|
+
|
393
|
+
$calendar_output .= '<td>';
|
394
|
+
|
395
|
+
|
396
|
+
|
397
|
+
if ( in_array($day, $daywithpost) ) // any posts today?
|
398
|
+
|
399
|
+
$calendar_output .= '<a href="' . get_day_link( $thisyear, $thismonth, $day ) . '?post_type='.$cpt.'" title="' . esc_attr( $ak_titles_for_day[ $day ] ) . "\">$day</a>";
|
400
|
+
|
401
|
+
else
|
402
|
+
|
403
|
+
$calendar_output .= $day;
|
404
|
+
|
405
|
+
$calendar_output .= '</td>';
|
406
|
+
|
407
|
+
|
408
|
+
|
409
|
+
if ( 6 == calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins) )
|
410
|
+
|
411
|
+
$newrow = true;
|
412
|
+
|
413
|
+
}
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
$pad = 7 - calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins);
|
418
|
+
|
419
|
+
if ( $pad != 0 && $pad != 7 )
|
420
|
+
|
421
|
+
$calendar_output .= "\n\t\t".'<td class="pad" colspan="'. esc_attr($pad) .'"> </td>';
|
422
|
+
|
423
|
+
|
424
|
+
|
425
|
+
$calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>";
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
$cache[ $key ] = $calendar_output;
|
430
|
+
|
431
|
+
wp_cache_set( 'get_calendar', $cache, 'calendar' );
|
432
|
+
|
433
|
+
|
434
|
+
|
435
|
+
if ( $echo )
|
436
|
+
|
437
|
+
echo apply_filters( 'get_calendar', $calendar_output );
|
438
|
+
|
439
|
+
else
|
440
|
+
|
441
|
+
return apply_filters( 'get_calendar', $calendar_output );
|
442
|
+
|
443
|
+
|
444
|
+
|
445
|
+
}
|
446
|
+
|
447
|
+
```
|
448
|
+
|
449
|
+
|
450
|
+
|
451
|
+
上記をfunctions.phpに記述し、
|
452
|
+
|
453
|
+
カレンダーを出力したい箇所に
|
454
|
+
|
455
|
+
```php
|
456
|
+
|
457
|
+
<?php get_cpt_calendar('投稿タイプ名'); ?>
|
458
|
+
|
459
|
+
```
|
460
|
+
|
461
|
+
上記を記述し呼び出したところ、
|
462
|
+
|
463
|
+
記事の投稿日が反映されたカレンダーが表示されております。
|
464
|
+
|
465
|
+
こちらをカスタムフィールドで指定した日付と関連づけたいというのは今回の希望です。
|
466
|
+
|
467
|
+
|
468
|
+
|
469
|
+
お力添えいただければ幸いです。
|
470
|
+
|
471
|
+
宜しくお願い致します。
|