質問編集履歴
1
使用コード・現状の出力結果の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -15,4 +15,222 @@
|
|
15
15
|
カスタムフィールドの日付と関連づけることが可能かを
|
16
16
|
ご教授いただきたいです。
|
17
17
|
|
18
|
+
宜しくお願い致します。
|
19
|
+
|
20
|
+
/*追記 180319
|
21
|
+
ご質問いただきました実際に使用しているコードは以下の通りです。
|
22
|
+
|
23
|
+
```php
|
24
|
+
function get_cpt_calendar($cpt, $initial = true, $echo = true) {
|
25
|
+
global $wpdb, $m, $monthnum, $year, $wp_locale, $posts;
|
26
|
+
|
27
|
+
$cache = array();
|
28
|
+
$key = md5( $m . $monthnum . $year );
|
29
|
+
if ( $cache = wp_cache_get( 'get_calendar', 'calendar' ) ) {
|
30
|
+
if ( is_array($cache) && isset( $cache[ $key ] ) ) {
|
31
|
+
if ( $echo ) {
|
32
|
+
echo apply_filters( 'get_calendar', $cache[$key] );
|
33
|
+
return;
|
34
|
+
} else {
|
35
|
+
return apply_filters( 'get_calendar', $cache[$key] );
|
36
|
+
}
|
37
|
+
}
|
38
|
+
}
|
39
|
+
|
40
|
+
if ( !is_array($cache) )
|
41
|
+
$cache = array();
|
42
|
+
|
43
|
+
// Quick check. If we have no posts at all, abort!
|
44
|
+
if ( !$posts ) {
|
45
|
+
$gotsome = $wpdb->get_var("SELECT 1 as test FROM $wpdb->posts WHERE post_type = '$cpt' AND post_status = 'publish' LIMIT 1");
|
46
|
+
if ( !$gotsome ) {
|
47
|
+
$cache[ $key ] = '';
|
48
|
+
wp_cache_set( 'get_calendar', $cache, 'calendar' );
|
49
|
+
return;
|
50
|
+
}
|
51
|
+
}
|
52
|
+
|
53
|
+
if ( isset($_GET['w']) )
|
54
|
+
$w = ''.intval($_GET['w']);
|
55
|
+
|
56
|
+
// week_begins = 0 stands for Sunday
|
57
|
+
$week_begins = intval(get_option('start_of_week'));
|
58
|
+
|
59
|
+
// Let's figure out when we are
|
60
|
+
if ( !empty($monthnum) && !empty($year) ) {
|
61
|
+
$thismonth = ''.zeroise(intval($monthnum), 2);
|
62
|
+
$thisyear = ''.intval($year);
|
63
|
+
} elseif ( !empty($w) ) {
|
64
|
+
// We need to get the month from MySQL
|
65
|
+
$thisyear = ''.intval(substr($m, 0, 4));
|
66
|
+
$d = (($w - 1) * 7) + 6; //it seems MySQL's weeks disagree with PHP's
|
67
|
+
$thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL $d DAY) ), '%m')");
|
68
|
+
} elseif ( !empty($m) ) {
|
69
|
+
$thisyear = ''.intval(substr($m, 0, 4));
|
70
|
+
if ( strlen($m) < 6 )
|
71
|
+
$thismonth = '01';
|
72
|
+
else
|
73
|
+
$thismonth = ''.zeroise(intval(substr($m, 4, 2)), 2);
|
74
|
+
} else {
|
75
|
+
$thisyear = gmdate('Y', current_time('timestamp'));
|
76
|
+
$thismonth = gmdate('m', current_time('timestamp'));
|
77
|
+
}
|
78
|
+
|
79
|
+
$unixmonth = mktime(0, 0 , 0, $thismonth, 1, $thisyear);
|
80
|
+
$last_day = date('t', $unixmonth);
|
81
|
+
|
82
|
+
// Get the next and previous month and year with at least one post
|
83
|
+
$previous = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
|
84
|
+
FROM $wpdb->posts
|
85
|
+
WHERE post_date < '$thisyear-$thismonth-01'
|
86
|
+
AND post_type = '$cpt' AND post_status = 'publish'
|
87
|
+
ORDER BY post_date DESC
|
88
|
+
LIMIT 1");
|
89
|
+
$next = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
|
90
|
+
FROM $wpdb->posts
|
91
|
+
WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59'
|
92
|
+
AND post_type = '$cpt' AND post_status = 'publish'
|
93
|
+
ORDER BY post_date ASC
|
94
|
+
LIMIT 1");
|
95
|
+
|
96
|
+
/* translators: Calendar caption: 1: month name, 2: 4-digit year */
|
97
|
+
$calendar_caption = _x('%1$s %2$s', 'calendar caption');
|
98
|
+
$calendar_output = '<table id="wp-calendar">
|
99
|
+
<caption>' . sprintf($calendar_caption, $wp_locale->get_month($thismonth), date('Y', $unixmonth)) . '</caption>
|
100
|
+
<thead>
|
101
|
+
<tr>';
|
102
|
+
|
103
|
+
$myweek = array();
|
104
|
+
|
105
|
+
for ( $wdcount=0; $wdcount<=6; $wdcount++ ) {
|
106
|
+
$myweek[] = $wp_locale->get_weekday(($wdcount+$week_begins)%7);
|
107
|
+
}
|
108
|
+
|
109
|
+
foreach ( $myweek as $wd ) {
|
110
|
+
$day_name = (true == $initial) ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd);
|
111
|
+
$wd = esc_attr($wd);
|
112
|
+
$calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>";
|
113
|
+
}
|
114
|
+
|
115
|
+
$calendar_output .= '
|
116
|
+
</tr>
|
117
|
+
</thead>
|
118
|
+
|
119
|
+
<tfoot>
|
120
|
+
<tr>';
|
121
|
+
|
122
|
+
if ( $previous ) {
|
123
|
+
$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>';
|
124
|
+
} else {
|
125
|
+
$calendar_output .= "\n\t\t".'<td colspan="3" id="prev" class="pad"> </td>';
|
126
|
+
}
|
127
|
+
|
128
|
+
$calendar_output .= "\n\t\t".'<td class="pad"> </td>';
|
129
|
+
|
130
|
+
if ( $next ) {
|
131
|
+
$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>';
|
132
|
+
} else {
|
133
|
+
$calendar_output .= "\n\t\t".'<td colspan="3" id="next" class="pad"> </td>';
|
134
|
+
}
|
135
|
+
|
136
|
+
$calendar_output .= '
|
137
|
+
</tr>
|
138
|
+
</tfoot>
|
139
|
+
|
140
|
+
<tbody>
|
141
|
+
<tr>';
|
142
|
+
|
143
|
+
// Get days with posts
|
144
|
+
$dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date)
|
145
|
+
FROM $wpdb->posts WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00'
|
146
|
+
AND post_type = '$cpt' AND post_status = 'publish'
|
147
|
+
AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", ARRAY_N);
|
148
|
+
if ( $dayswithposts ) {
|
149
|
+
foreach ( (array) $dayswithposts as $daywith ) {
|
150
|
+
$daywithpost[] = $daywith[0];
|
151
|
+
}
|
152
|
+
} else {
|
153
|
+
$daywithpost = array();
|
154
|
+
}
|
155
|
+
|
156
|
+
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'camino') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'safari') !== false)
|
157
|
+
$ak_title_separator = "\n";
|
158
|
+
else
|
159
|
+
$ak_title_separator = ', ';
|
160
|
+
|
161
|
+
$ak_titles_for_day = array();
|
162
|
+
$ak_post_titles = $wpdb->get_results("SELECT ID, post_title, DAYOFMONTH(post_date) as dom "
|
163
|
+
."FROM $wpdb->posts "
|
164
|
+
."WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00' "
|
165
|
+
."AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59' "
|
166
|
+
."AND post_type = '$cpt' AND post_status = 'publish'"
|
167
|
+
);
|
168
|
+
if ( $ak_post_titles ) {
|
169
|
+
foreach ( (array) $ak_post_titles as $ak_post_title ) {
|
170
|
+
|
171
|
+
/** This filter is documented in wp-includes/post-template.php */
|
172
|
+
$post_title = esc_attr( apply_filters( 'the_title', $ak_post_title->post_title, $ak_post_title->ID ) );
|
173
|
+
|
174
|
+
if ( empty($ak_titles_for_day['day_'.$ak_post_title->dom]) )
|
175
|
+
$ak_titles_for_day['day_'.$ak_post_title->dom] = '';
|
176
|
+
if ( empty($ak_titles_for_day["$ak_post_title->dom"]) ) // first one
|
177
|
+
$ak_titles_for_day["$ak_post_title->dom"] = $post_title;
|
178
|
+
else
|
179
|
+
$ak_titles_for_day["$ak_post_title->dom"] .= $ak_title_separator . $post_title;
|
180
|
+
}
|
181
|
+
}
|
182
|
+
|
183
|
+
// See how much we should pad in the beginning
|
184
|
+
$pad = calendar_week_mod(date('w', $unixmonth)-$week_begins);
|
185
|
+
if ( 0 != $pad )
|
186
|
+
$calendar_output .= "\n\t\t".'<td colspan="'. esc_attr($pad) .'" class="pad"> </td>';
|
187
|
+
|
188
|
+
$daysinmonth = intval(date('t', $unixmonth));
|
189
|
+
for ( $day = 1; $day <= $daysinmonth; ++$day ) {
|
190
|
+
if ( isset($newrow) && $newrow )
|
191
|
+
$calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t";
|
192
|
+
$newrow = false;
|
193
|
+
|
194
|
+
if ( $day == gmdate('j', current_time('timestamp')) && $thismonth == gmdate('m', current_time('timestamp')) && $thisyear == gmdate('Y', current_time('timestamp')) )
|
195
|
+
$calendar_output .= '<td id="today">';
|
196
|
+
else
|
197
|
+
$calendar_output .= '<td>';
|
198
|
+
|
199
|
+
if ( in_array($day, $daywithpost) ) // any posts today?
|
200
|
+
$calendar_output .= '<a href="' . get_day_link( $thisyear, $thismonth, $day ) . '?post_type='.$cpt.'" title="' . esc_attr( $ak_titles_for_day[ $day ] ) . "\">$day</a>";
|
201
|
+
else
|
202
|
+
$calendar_output .= $day;
|
203
|
+
$calendar_output .= '</td>';
|
204
|
+
|
205
|
+
if ( 6 == calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins) )
|
206
|
+
$newrow = true;
|
207
|
+
}
|
208
|
+
|
209
|
+
$pad = 7 - calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins);
|
210
|
+
if ( $pad != 0 && $pad != 7 )
|
211
|
+
$calendar_output .= "\n\t\t".'<td class="pad" colspan="'. esc_attr($pad) .'"> </td>';
|
212
|
+
|
213
|
+
$calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>";
|
214
|
+
|
215
|
+
$cache[ $key ] = $calendar_output;
|
216
|
+
wp_cache_set( 'get_calendar', $cache, 'calendar' );
|
217
|
+
|
218
|
+
if ( $echo )
|
219
|
+
echo apply_filters( 'get_calendar', $calendar_output );
|
220
|
+
else
|
221
|
+
return apply_filters( 'get_calendar', $calendar_output );
|
222
|
+
|
223
|
+
}
|
224
|
+
```
|
225
|
+
|
226
|
+
上記をfunctions.phpに記述し、
|
227
|
+
カレンダーを出力したい箇所に
|
228
|
+
```php
|
229
|
+
<?php get_cpt_calendar('投稿タイプ名'); ?>
|
230
|
+
```
|
231
|
+
上記を記述し呼び出したところ、
|
232
|
+
記事の投稿日が反映されたカレンダーが表示されております。
|
233
|
+
こちらをカスタムフィールドで指定した日付と関連づけたいというのは今回の希望です。
|
234
|
+
|
235
|
+
お力添えいただければ幸いです。
|
18
236
|
宜しくお願い致します。
|