回答編集履歴

1

2017/10/04 13:48

投稿

退会済みユーザー
test CHANGED
@@ -6,4 +6,408 @@
6
6
 
7
7
 
8
8
 
9
+ **追記**
10
+
11
+ 例を載せておくので弄ってみてください。
12
+
13
+ あくまでコアのコードをそのまま流用してるだけなのでtdで出力するようになってますが、希望はspanとの事なのでtdへspanを放りこんでるので、my_calendar_spanを探して好きなようにしてみてください。
14
+
15
+
16
+
17
+ aタグの方はclassをmy_calendar_classにしてあるので探してください。
18
+
19
+ aタグの方もtdで出力されるので```my_calendar_span```が入りますが、これは致し方ないでしょう。
20
+
21
+
22
+
23
+
24
+
25
+ 以下をテーマのfunctions.phpに追加。
26
+
27
+ ```
28
+
29
+ function get_my_calendar( $initial = true, $echo = true ) {
30
+
31
+ global $wpdb, $m, $monthnum, $year, $wp_locale, $posts;
32
+
33
+
34
+
35
+ $key = md5( $m . $monthnum . $year );
36
+
37
+ $cache = wp_cache_get( 'get_calendar', 'calendar' );
38
+
39
+
40
+
41
+ if ( $cache && is_array( $cache ) && isset( $cache[ $key ] ) ) {
42
+
43
+ $output = apply_filters( 'get_calendar', $cache[ $key ] );
44
+
45
+
46
+
47
+ if ( $echo ) {
48
+
49
+ echo $output;
50
+
51
+ return;
52
+
53
+ }
54
+
55
+
56
+
57
+ return $output;
58
+
59
+ }
60
+
61
+
62
+
63
+ if ( ! is_array( $cache ) ) {
64
+
65
+ $cache = array();
66
+
67
+ }
68
+
69
+
70
+
71
+ if ( ! $posts ) {
72
+
73
+ $gotsome = $wpdb->get_var("SELECT 1 as test FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1");
74
+
75
+ if ( ! $gotsome ) {
76
+
77
+ $cache[ $key ] = '';
78
+
79
+ wp_cache_set( 'get_calendar', $cache, 'calendar' );
80
+
81
+ return;
82
+
83
+ }
84
+
85
+ }
86
+
87
+
88
+
89
+ if ( isset( $_GET['w'] ) ) {
90
+
91
+ $w = (int) $_GET['w'];
92
+
93
+ }
94
+
95
+ $week_begins = (int) get_option( 'start_of_week' );
96
+
97
+ $ts = current_time( 'timestamp' );
98
+
99
+
100
+
101
+ if ( ! empty( $monthnum ) && ! empty( $year ) ) {
102
+
103
+ $thismonth = zeroise( intval( $monthnum ), 2 );
104
+
105
+ $thisyear = (int) $year;
106
+
107
+ } elseif ( ! empty( $w ) ) {
108
+
109
+ $thisyear = (int) substr( $m, 0, 4 );
110
+
111
+ $d = ( ( $w - 1 ) * 7 ) + 6;
112
+
113
+ $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL $d DAY) ), '%m')");
114
+
115
+ } elseif ( ! empty( $m ) ) {
116
+
117
+ $thisyear = (int) substr( $m, 0, 4 );
118
+
119
+ if ( strlen( $m ) < 6 ) {
120
+
121
+ $thismonth = '01';
122
+
123
+ } else {
124
+
125
+ $thismonth = zeroise( (int) substr( $m, 4, 2 ), 2 );
126
+
127
+ }
128
+
129
+ } else {
130
+
131
+ $thisyear = gmdate( 'Y', $ts );
132
+
133
+ $thismonth = gmdate( 'm', $ts );
134
+
135
+ }
136
+
137
+
138
+
139
+ $unixmonth = mktime( 0, 0 , 0, $thismonth, 1, $thisyear );
140
+
141
+ $last_day = date( 't', $unixmonth );
142
+
143
+
144
+
145
+ $previous = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
146
+
147
+ FROM $wpdb->posts
148
+
149
+ WHERE post_date < '$thisyear-$thismonth-01'
150
+
151
+ AND post_type = 'post' AND post_status = 'publish'
152
+
153
+ ORDER BY post_date DESC
154
+
155
+ LIMIT 1");
156
+
157
+ $next = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
158
+
159
+ FROM $wpdb->posts
160
+
161
+ WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59'
162
+
163
+ AND post_type = 'post' AND post_status = 'publish'
164
+
165
+ ORDER BY post_date ASC
166
+
167
+ LIMIT 1");
168
+
169
+
170
+
171
+ $calendar_caption = _x('%1$s %2$s', 'calendar caption');
172
+
173
+ $calendar_output = '<table id="wp-calendar">
174
+
175
+ <caption>' . sprintf(
176
+
177
+ $calendar_caption,
178
+
179
+ $wp_locale->get_month( $thismonth ),
180
+
181
+ date( 'Y', $unixmonth )
182
+
183
+ ) . '</caption>
184
+
185
+ <thead>
186
+
187
+ <tr>';
188
+
189
+
190
+
191
+ $myweek = array();
192
+
193
+
194
+
195
+ for ( $wdcount = 0; $wdcount <= 6; $wdcount++ ) {
196
+
197
+ $myweek[] = $wp_locale->get_weekday( ( $wdcount + $week_begins ) % 7 );
198
+
199
+ }
200
+
201
+
202
+
203
+ foreach ( $myweek as $wd ) {
204
+
205
+ $day_name = $initial ? $wp_locale->get_weekday_initial( $wd ) : $wp_locale->get_weekday_abbrev( $wd );
206
+
207
+ $wd = esc_attr( $wd );
208
+
209
+ $calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>";
210
+
211
+ }
212
+
213
+
214
+
215
+ $calendar_output .= '
216
+
217
+ </tr>
218
+
219
+ </thead>
220
+
221
+
222
+
223
+ <tfoot>
224
+
225
+ <tr>';
226
+
227
+
228
+
229
+ if ( $previous ) {
230
+
231
+ $calendar_output .= "\n\t\t".'<td colspan="3" id="prev"><a href="' . get_month_link( $previous->year, $previous->month ) . '">&laquo; ' .
232
+
233
+ $wp_locale->get_month_abbrev( $wp_locale->get_month( $previous->month ) ) .
234
+
235
+ '</a></td>';
236
+
237
+ } else {
238
+
239
+ $calendar_output .= "\n\t\t".'<td colspan="3" id="prev" class="pad">&nbsp;</td>';
240
+
241
+ }
242
+
243
+
244
+
245
+ $calendar_output .= "\n\t\t".'<td class="pad">&nbsp;</td>';
246
+
247
+
248
+
249
+ if ( $next ) {
250
+
251
+ $calendar_output .= "\n\t\t".'<td colspan="3" id="next"><a href="' . get_month_link( $next->year, $next->month ) . '">' .
252
+
253
+ $wp_locale->get_month_abbrev( $wp_locale->get_month( $next->month ) ) .
254
+
255
+ ' &raquo;</a></td>';
256
+
257
+ } else {
258
+
259
+ $calendar_output .= "\n\t\t".'<td colspan="3" id="next" class="pad">&nbsp;</td>';
260
+
261
+ }
262
+
263
+
264
+
265
+ $calendar_output .= '
266
+
267
+ </tr>
268
+
269
+ </tfoot>
270
+
271
+
272
+
273
+ <tbody>
274
+
275
+ <tr>';
276
+
277
+
278
+
279
+ $daywithpost = array();
280
+
281
+
282
+
283
+ $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date)
284
+
285
+ FROM $wpdb->posts WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00'
286
+
287
+ AND post_type = 'post' AND post_status = 'publish'
288
+
289
+ AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", ARRAY_N);
290
+
291
+ if ( $dayswithposts ) {
292
+
293
+ foreach ( (array) $dayswithposts as $daywith ) {
294
+
295
+ $daywithpost[] = $daywith[0];
296
+
297
+ }
298
+
299
+ }
300
+
301
+
302
+
303
+ $pad = calendar_week_mod( date( 'w', $unixmonth ) - $week_begins );
304
+
305
+ if ( 0 != $pad ) {
306
+
307
+ $calendar_output .= "\n\t\t".'<td colspan="'. esc_attr( $pad ) .'" class="pad">&nbsp;</td>';
308
+
309
+ }
310
+
311
+
312
+
313
+ $newrow = false;
314
+
315
+ $daysinmonth = (int) date( 't', $unixmonth );
316
+
317
+
318
+
319
+ for ( $day = 1; $day <= $daysinmonth; ++$day ) {
320
+
321
+ if ( isset($newrow) && $newrow ) {
322
+
323
+ $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t";
324
+
325
+ }
326
+
327
+ $newrow = false;
328
+
329
+
330
+
331
+ if ( $day == gmdate( 'j', $ts ) &&
332
+
333
+ $thismonth == gmdate( 'm', $ts ) &&
334
+
335
+ $thisyear == gmdate( 'Y', $ts ) ) {
336
+
337
+ $calendar_output .= '<td id="today">';
338
+
339
+ } else {
340
+
341
+ $calendar_output .= '<td><span class="my_calendar_span">';
342
+
343
+ }
344
+
345
+
346
+
347
+ if ( in_array( $day, $daywithpost ) ) {
348
+
349
+ $date_format = date( _x( 'F j, Y', 'daily archives date format' ), strtotime( "{$thisyear}-{$thismonth}-{$day}" ) );
350
+
351
+ $label = sprintf( __( 'Posts published on %s' ), $date_format );
352
+
353
+ $calendar_output .= sprintf(
354
+
355
+ '<a class="my_calendar_class" href="%s" aria-label="%s">%s</a>',
356
+
357
+ get_day_link( $thisyear, $thismonth, $day ),
358
+
359
+ esc_attr( $label ),
360
+
361
+ $day
362
+
363
+ );
364
+
365
+ } else {
366
+
367
+ $calendar_output .= $day;
368
+
369
+ }
370
+
371
+ $calendar_output .= '</span></td>';
372
+
373
+
374
+
375
+ if ( 6 == calendar_week_mod( date( 'w', mktime(0, 0 , 0, $thismonth, $day, $thisyear ) ) - $week_begins ) ) {
376
+
377
+ $newrow = true;
378
+
379
+ }
380
+
381
+ }
382
+
383
+
384
+
385
+ $pad = 7 - calendar_week_mod( date( 'w', mktime( 0, 0 , 0, $thismonth, $day, $thisyear ) ) - $week_begins );
386
+
387
+ if ( $pad != 0 && $pad != 7 ) {
388
+
389
+ $calendar_output .= "\n\t\t".'<td class="pad" colspan="'. esc_attr( $pad ) .'">&nbsp;</td>';
390
+
391
+ }
392
+
393
+ $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>";
394
+
395
+
396
+
397
+ $cache[ $key ] = $calendar_output;
398
+
399
+ wp_cache_set( 'get_calendar', $cache, 'calendar' );
400
+
401
+
402
+
403
+ if ( $echo ) {
404
+
405
+ return apply_filters( 'get_calendar', $calendar_output );
406
+
407
+ }
408
+
409
+ }
410
+
411
+ ```
412
+
9
- スタマイズしたカレンダーの呼び出```<?php get_my_calendar(); ?>```で。
413
+ カレンダーを表示たいところに```<?php echo get_my_calendar(); ?>```を書き込めば表示されるはず